[R] reshape()

Robin Hankin r.hankin at auckland.ac.nz
Tue Sep 3 00:20:55 CEST 2002


Dear Helplist

I have a dataframe that holds the Southern Oscillation Index over the
last few years:

R> soi[1:3,]
  Year Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1 1993  -9 -10 -12 -24  -9 -18 -11 -18  -9 -15  -1   0
2 1994  -2  -1 -14 -26 -13 -12 -18 -20 -19 -16  -9 -15
3 1995  -4  -5   2 -19  -9  -3   4  -1   3  -2   0  -8


QUESTION: how do I coerce reshape() into giving me this:

   Year Month SOI
1  1993  Jan   -9
2  1993  Feb  -10
3  1993  Mar  -12
4  1993  Apr  -24
...
36 1995  Dec   -8

(or indeed, is there a better method than reshape()?).  I just can't
seem to find the right combination of times= and idvar= to work.

thanks in advance
rksh


R> dput(soi)
structure(list(Year = c(1993, 1994, 1995, 1996, 1997, 1998, 1999, 
2000, 2001, 2002), Jan = c(-9, -2, -4, 9, 5, -26, 17, 6, 10, 
3), Feb = c(-10, -1, -5, -1, 12, -22, 7, 12, 11, 6), Mar = c(-12, 
-14, 2, 5, -12, -35, 8, 8, 5, -8), Apr = c(-24, -26, -19, 9, 
-19, -28, 21, 19, 0, -5), May = c(-9, -13, -9, 1, -23, 0, 1, 
4, -9, -15), Jun = c(-18, -12, -3, 14, -27, 9, 0, -7, 1, -8), 
    Jul = c(-11, -18, 4, 7, -10, 15, 5, -4, -3, -8), Aug = c(-18, 
    -20, -1, 3, -23, 9, 1, 4, -11, NA), Sep = c(-9, -19, 3, 6, 
    -16, 11, -1, 9, 1, NA), Oct = c(-15, -16, -2, 3, -20, 10, 
    8, 9, -3, NA), Nov = c(-1, -9, 0, -1, -18, 12, 13, 23, 6, 
    NA), Dec = c(0, -15, -8, 6, -12, 13, 12, 7, -12, NA)), .Names = c("Year", 
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", 
"Oct", "Nov", "Dec"), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10"))
R> 




-- 

Robin Hankin, Lecturer,
School of Geography and Environmental Science
Tamaki Campus
Private Bag 92019 Auckland
New Zealand

r.hankin at auckland.ac.nz
tel 0064-9-373-7599 x6820; FAX 0064-9-373-7042

as of: Tue Sep  3 10:16:00 NZST 2002
This (linux) system up continuously for:  369 days, 16 hours, 58 minutes
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list