[R] reshape()

Thomas Lumley tlumley at u.washington.edu
Tue Sep 3 03:31:34 CEST 2002


On Tue, 3 Sep 2002, Robin Hankin wrote:

> 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
>

months<-names(soi)[-1]
reshape(soi,timevar="Year",varying=list(months),times=months,direction="long")

This produces an unnecessary but harmless "id" variable as well.
	-thomas

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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