[R] Help with reshape/reShape and indexing

hadley wickham h.wickham at gmail.com
Wed May 13 14:13:01 CEST 2009


> This does it more or less your way:
>
> ds <- split(df, df$Name)
> ds <- lapply(ds, function(x){x$Index <- seq_along(x[,1]); x})
> df2 <- unsplit(ds, df$Name)
> tapply(df2$X1, df2[,c("Name", "Index")], function(x) x)
>
> athough there may exist much easier ways ...

Here's one way with the plyr and reshape package:

library(plyr)
df.index <- ddply(df, .(Name), transform, Index = seq_along(X1))

library(reshape)
cast(df.index, Name ~ Index, value = "X1")

Hadley



-- 
http://had.co.nz/




More information about the R-help mailing list