[R] reshape help

Peter Dalgaard p.dalgaard at biostat.ku.dk
Wed Sep 12 23:02:04 CEST 2007


Chuck Cleland wrote:
> juli pausas wrote:
>   
>> Hi,
>> I'm trying to use reshape but I cannot quite understand how it works.
>> Could somebody help me on this? Example, my data is something like:
>>
>> mydat <- data.frame(tree= 1:10, serra=rep(1:2, c(5,5)), bt01= 101:110,
>> bt02= 201:210, bt03= 301:310,  mm01= 9101:9110, mm02= 9201:9210, mm03=
>> 9301:9310)
>>
>>     
>>> mydat
>>>       
>>    tree serra bt01 bt02 bt03 mm01 mm02 mm03
>> 1     1     1  101  201  301  9101  9201  9301
>> 2     2     1  102  202  302  9102  9202  9302
>> 3     3     1  103  203  303  9103  9203  9303
>> 4     4     1  104  204  304  9104  9204  9304
>> 5     5     1  105  205  305  9105  9205  9305
>> 6     6     2  106  206  306  9106  9206  9306
>> 7     7     2  107  207  307  9107  9207  9307
>> 8     8     2  108  208  308  9108  9208  9308
>> 9     9     2  109  209  309  9109  9209  9309
>> 10   10     2  110  210  310  9110  9210  9310
>>
>> that is, in the "wide form" with 2 constant variables (tree, serra)
>> and 6 variables that correspond to two variables (bt mm) measured in 3
>> years 01, 02, 03
>>
>> I would like to reshaped the data to the long format as follows:
>>
>>   tree serra   YEAR   bt   mm
>>      1     1    2001   101   9101
>>      2     1    2001   102   9102
>>      3     1    2001   103   9103
>>      4     1    2001   104   9104
>>      5     1    2001   105   9105
>>      6     2    2001   106   9106
>>      7     2    2001   107   9107
>>      8     2    2001   108   9108
>>      9     2    2001   109   9109
>>     10     2    2001   110   9110
>>      1     1    2002   201   9201
>>      2     1    2002   202   9202
>>      3     1    2002   203   9203
>>      4     1    2002   204   9204
>>      5     1    2002   205   9205
>>      6     2    2002   206   9206
>>      7     2    2002   207   9207
>>      8     2    2002   208   9208
>>      9     2    2002   209   9209
>>     10     2    2002   210   9210
>>      1     1    2003   301   9301
>>      2     1    2003   302   9302
>>      3     1    2003   303   9303
>>      4     1    2003   304   9304
>>      5     1    2003   305   9305
>>      6     2    2003   306   9306
>>      7     2    2003   307   9307
>>      8     2    2003   308   9308
>>      9     2    2003   309   9309
>>     10     2    2003   310   9310
>>
>> I would appreciate if somebody let me know how could I do this with reshape
>>     
>
> reshape(mydat, varying = list(c("bt01","bt02","bt03"),
>                               c("mm01","mm02","mm03")),
>                v.names=c("bt","mm"),
>                timevar = "YEAR",
>                times = c(2001, 2002, 2003),
>                idvar = "tree",
>                direction = "long")
>   
Yup. 2.6.0 will also allow this simplified variant:

 > reshape(mydat, direction="long", varying=3:8, sep="")
     tree serra time  bt   mm id
1.1     1     1    1 101 9101  1
2.1     2     1    1 102 9102  2
3.1     3     1    1 103 9103  3
4.1     4     1    1 104 9104  4
5.1     5     1    1 105 9105  5
6.1     6     2    1 106 9106  6
7.1     7     2    1 107 9107  7
8.1     8     2    1 108 9108  8
9.1     9     2    1 109 9109  9
10.1   10     2    1 110 9110 10
1.2     1     1    2 201 9201  1
2.2     2     1    2 202 9202  2
3.2     3     1    2 203 9203  3
4.2     4     1    2 204 9204  4
5.2     5     1    2 205 9205  5
6.2     6     2    2 206 9206  6
7.2     7     2    2 207 9207  7
8.2     8     2    2 208 9208  8
9.2     9     2    2 209 9209  9
10.2   10     2    2 210 9210 10
1.3     1     1    3 301 9301  1
2.3     2     1    3 302 9302  2
3.3     3     1    3 303 9303  3
4.3     4     1    3 304 9304  4
5.3     5     1    3 305 9305  5
6.3     6     2    3 306 9306  6
7.3     7     2    3 307 9307  7
8.3     8     2    3 308 9308  8
9.3     9     2    3 309 9309  9
10.3   10     2    3 310 9310 10



-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907



More information about the R-help mailing list