Skip to content
Prev 168751 / 398502 Next

reshape with two time variables

hadley wickham wrote:
Also, things like this should work (here using stock reshape):

 > xx <- reshape(x, direction="long", varying=2:5, v.names="val")
 > cbind(xx, expand.grid(B=1:2,A=1:2)[xx$time,])
     id time val B A
1.1  1    1 400 1 1
2.1  2    1 390 1 1
3.1  3    1 428 1 1
4.1  4    1 703 1 1
5.1  5    1 611 1 1
6.1  6    1 543 1 1
7.1  7    1 411 1 1
8.1  8    1 654 1 1
1.2  1    2 475 2 1
2.2  2    2 500 2 1
3.2  3    2 512 2 1
4.2  4    2 787 2 1
5.2  5    2 634 2 1
...

Once you got your mind wrapped around it, the pattern might be easier to 
generalize to more complex within-subject designs.