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") tree serra YEAR bt mm 1.2001 1 1 2001 101 9101 2.2001 2 1 2001 102 9102 3.2001 3 1 2001 103 9103 4.2001 4 1 2001 104 9104 5.2001 5 1 2001 105 9105 6.2001 6 2 2001 106 9106 7.2001 7 2 2001 107 9107 8.2001 8 2 2001 108 9108 9.2001 9 2 2001 109 9109 10.2001 10 2 2001 110 9110 1.2002 1 1 2002 201 9201 2.2002 2 1 2002 202 9202 3.2002 3 1 2002 203 9203 4.2002 4 1 2002 204 9204 5.2002 5 1 2002 205 9205 6.2002 6 2 2002 206 9206 7.2002 7 2 2002 207 9207 8.2002 8 2 2002 208 9208 9.2002 9 2 2002 209 9209 10.2002 10 2 2002 210 9210 1.2003 1 1 2003 301 9301 2.2003 2 1 2003 302 9302 3.2003 3 1 2003 303 9303 4.2003 4 1 2003 304 9304 5.2003 5 1 2003 305 9305 6.2003 6 2 2003 306 9306 7.2003 7 2 2003 307 9307 8.2003 8 2 2003 308 9308 9.2003 9 2 2003 309 9309 10.2003 10 2 2003 310 9310 > Thanks in advance > > Juli -- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894 ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.