Skip to content
Prev 77503 / 398502 Next

Transform variable number of rows per subject to column variables?

On 9/20/05, Bing Ho <2bingho at stanford.edu> wrote:
First manufacture a "time" column and then use reshape:

tt <- sequence(rle(DF$ID)$lengths)
reshape(cbind(tt, DF), idvar = "ID", timevar = "tt", direction = "wide")

Another possibility is to use the reshape package:

library(reshape)
DF.d <- deshape(cbind(tt, DF), id = 1:2)  # same tt as above
reshape(DF.d, ID ~ variable + tt)