0

I have a data set with three columns The first column includes variable names, the second column include time stamps, and the third column is the value For example

var1, 2015-1-1, 20
var1, 2015-1-2, 30
var2, 2015-1-1, 40
var2, 2015-1-2, 50

I'd like to convert the data set from this format to a format with the first column of time stamp, and the rest columns for each variable value. Can anyone provide a hint using R data.table or reshape package? For example:

2015-1-1, 20, 40
2015-1-2, 30, 50
Burak
  • 5,252
  • 3
  • 22
  • 30
athlonshi
  • 1,711
  • 1
  • 19
  • 23
  • 1
    Try `library(reshape2);dcast(df1, V2~V1, value.var='V3')` where `V1:V3` are the column names in the dataset `df1` – akrun Jul 15 '15 at 21:48
  • 1
    `data.table` also has a `dcast` method so @akruns code will work with `dcast.data.table(setDT(df1)...` if you want it in a `data.table` way. – David Arenburg Jul 15 '15 at 21:51
  • 1
    [this](http://stackoverflow.com/questions/9617348/reshape-three-column-data-frame-to-matrix) is also a good reference, – David Arenburg Jul 15 '15 at 21:53
  • Thanks a lot. These work great. data.table is awesome and very fast. – athlonshi Jul 20 '15 at 03:55

0 Answers0