[R] reshaping data frame

Vikas Rawal vikasrawal at gmail.com
Fri Feb 22 02:32:40 CET 2008


> # 2. create one new row for each case in level.1 and level.2
> 
> # the new reshaped data.frame would should look like this:
> 
> # indiv  factor    covar   case.id
> #   A   level.1   4.614105    1
> #   A   level.1   4.614105    2
> #   A   level.2  31.064405    1
> #   A   level.2  31.064405    2
> #   A   level.2  31.064405    3
> #   A   level.2  31.064405    4
> #   A   level.1  19.185784    1
> #   A   level.2  48.455929    1
> #   A   level.2  48.455929    2
> #   A   level.2  48.455929    3
> # etc...
> 
> #############################


This works for me.

melt(my.dat,id=c("indiv","covar.1","covar.2"))->my.dat.1
names(my.dat.1)[4:5]<-c("level","case.id")
melt(my.dat.1,id=c("indiv","level","case.id"))->my.dat.2

Vikas



More information about the R-help mailing list