Created
October 27, 2016 20:16
-
-
Save tgh0831/772d5b3a52e804dcd4efbf679357ac62 to your computer and use it in GitHub Desktop.
This is an example of pivoting (casting) a data frame.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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