I'm trying to do exactly what reshape from the stats package is designed for. I have a wide dataset with a series of variables in the form var_name.date
. Unfortunately, reshape seems ill-equipped to deal with even medium-sized datasets, so I'm trying to use the the data.table.melt
function.
My main problem is grouping the variables into separate value columns based on their long-form variable. Is this possible, or do I need to do each one separately and then cbind
them?
Here is what I have:
widetable = data.table("id"=1:5,"A.2012-10"=runif(5),"A.2012-11"=runif(5),
"B.2012-10"=runif(5),"B.2012-11"=runif(5))
id A.2012-10 A.2012-11 B.2012-10 B.2012-11
1: 1 0.82982349 0.2257782 0.46390924 0.4448248
2: 2 0.46136746 0.2184797 0.05640388 0.4772663
3: 3 0.61723234 0.3950625 0.03252784 0.4006974
4: 4 0.19963437 0.7028052 0.06811452 0.3096969
5: 5 0.09575389 0.5510507 0.76059610 0.8630222
And here is the the stats
package's reshape
mocking me with one-line awesomeness doing exactly what I want but not scaling.
reshape(widetable, idvar="id", varying=colnames(widetable)[2:5],
sep=".", direction="long")
id time A B
1: 1 2012-10 0.82982349 0.46390924
2: 2 2012-10 0.46136746 0.05640388
3: 3 2012-10 0.61723234 0.03252784
4: 4 2012-10 0.19963437 0.06811452
5: 5 2012-10 0.09575389 0.76059610
6: 1 2012-11 0.22577823 0.44482478
7: 2 2012-11 0.21847969 0.47726629
8: 3 2012-11 0.39506249 0.40069737
9: 4 2012-11 0.70280519 0.30969695
10: 5 2012-11 0.55105075 0.86302220