[R] reshape question

hadley wickham h.wickham at gmail.com
Wed Nov 25 04:05:19 CET 2009


> I don't really understand what you want and the example solution throws away
> quite a lot of data, so consider this alternative:
>
> data.out2 <- read.table(textConnection("id   rater.1 n.1   rater.2 n.2
> rater.3 n.3   rater.4 n.4
> 11   11 0.1183333  79        NA  NA        NA  NA        NA  NA
> 114 114 0.2478709 113        NA  NA        NA  NA        NA  NA
> 12   12 0.3130655  54 0.3668242  54        NA  NA        NA  NA
> 121 121 0.2400000 331        NA  NA        NA  NA        NA  NA
> 122 122 0.3004164  25 0.1046278  25 0.2424871  25 0.2796937  25
> 125 125 0.1634865 190        NA  NA        NA  NA        NA  NA"), header=T,
> stringsAsFactors=F)
>

Or

library(reshape)
df <- melt(data.out2, na.rm = T, id = "id")
df <- cbind(df, colsplit(df$variable, "\\.", c("var", "time")))

cast(df, id + time ~ var)

See http://had.co.nz/reshape for more details.

Hadley

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




More information about the R-help mailing list