5

plyr、、関数および/またはreshape2を使用してこれをよりエレガントに行う方法は?aggregatedata.table

library(plyr)

set.seed(1) 
x <- data.frame(Ind = paste0("Ind", 1:10), Treatment = c(rep("Treat",10),rep("Cont",10)),
value = rnorm(20,60,8))

tr <- subset(x, Treatment == "Treat")
tr <- rename(tr, c("value" = "Treat"))

ct <- subset(x, Treatment == "Cont")
ct <- rename(ct, c("value" = "Cont"))

merge(ct[-2], tr[-2], by = "Ind", all = T, sort = F)

# Do not run, data.frame:
     Ind     Cont    Treat
1   Ind1 72.09425 54.98837
2   Ind2 63.11875 61.46915
3   Ind3 55.03008 53.31497
4   Ind4 42.28240 72.76225
5   Ind5 68.99945 62.63606
6   Ind6 59.64053 53.43625
7   Ind7 59.87048 63.89943
8   Ind8 67.55069 65.90660
9   Ind9 66.56977 64.60625
10 Ind10 64.75121 57.55689
4

3 に答える 3

6

dcast()ライブラリの関数を使用できますreshape2

 dcast(data=x,Ind~Treatment)
     Ind     Cont    Treat
1   Ind1 53.45988 53.68913
2  Ind10 54.02344 66.32866
3   Ind2 57.44591 62.32354
4   Ind3 67.53185 53.14807
5   Ind4 52.42713 55.04052
6   Ind5 63.80633 61.58893
7   Ind6 59.40308 51.66228
8   Ind7 67.79597 73.60620
9   Ind8 58.15420 65.06976
10  Ind9 61.45161 63.73947
于 2013-04-16T09:07:01.927 に答える