Skip to content

Instantly share code, notes, and snippets.

@tgh0831
Created October 27, 2016 20:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tgh0831/772d5b3a52e804dcd4efbf679357ac62 to your computer and use it in GitHub Desktop.
Save tgh0831/772d5b3a52e804dcd4efbf679357ac62 to your computer and use it in GitHub Desktop.
This is an example of pivoting (casting) a data frame.
require(data.table) #data.table is only required to create the sample data frame
require(reshape2) #dcast is a function in the reshape2 package
#generate a data table with some records
df <- data.table(ServerName = c('SERVER1','SERVER2','SERVER3','SERVER4'),RecordDate = seq(as.Date("2011-12-30"), as.Date("2012-01-11"), by="days"),Value = seq(0, 30, by = 2.35))
#Pivot the result, force the value to be the "Value" field
df <- dcast(df, RecordDate ~ ServerName, value.var = 'Value')
#Get rid of NA values in the data frame
df[is.na(df)] <- ''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment