[R] reshape help

juli pausas pausas at gmail.com
Wed Sep 12 21:58:53 CEST 2007


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
Thanks in advance

Juli


-- 
http://www.ceam.es/pausas



More information about the R-help mailing list