出於某種原因,我得到的結果比我預期的更高,因爲升級到R-2.13.0 - 並且升級到plyr_1.5.1.tar.gz ...我在舊版本上嘗試過版本plyr的(版本不確定不幸的是,因爲我已經僅覆蓋了吧...)ddply返回的結果太多
library(plyr)
dd <-data.frame(matrix(rnorm(216),72,3),c(rep("A",24),rep("B",24),
rep("C",24)),c(rep("J",36),rep("K",36)))
colnames(dd) <- c("v1", "v2", "v3", "dim1", "dim2")
results1 <- ddply(dd, c("dim1","dim2"), function(df) c(m1=mean(df$v1)))
results2 <- ddply(dd, c("dim1","dim2"), function(df) { c(m1=mean(df$v1),
m2=mean(df$v2)) })
results3 <- ddply(dd, c("dim1","dim2"), function(df) { c(m1=mean(df$v1),
m2=mean(df$v2), m3=mean(df$v3)) })
我不明白,爲什麼結果2有結果1行數的兩倍,並有結果3的三倍多 - 其中原始結果1僅複製兩次或三次。
我使用的是舊版本plyr的,我期待爲結果[R版本,方便的拷貝2.11.0修補的(2010-05-01 r51907)...
> results1
dim1 dim2 m1
1 A J 0.07312783
2 B J -0.22428746
3 B K -0.44205832
4 C K 0.21421456
> results2
dim1 dim2 m1 m2
1 A J 0.07312783 -0.1130148
2 B J -0.22428746 0.4394832
3 B K -0.44205832 -0.1934018
4 C K 0.21421456 -0.0178809
> results3
dim1 dim2 m1 m2 m3
1 A J 0.07312783 -0.1130148 -0.03175873
2 B J -0.22428746 0.4394832 0.21581696
3 B K -0.44205832 -0.1934018 -0.28313530
4 C K 0.21421456 -0.0178809 -0.21948430
結果我得到從R版本2.13.0(2011-04-13)
> results1
dim1 dim2 m1
1 A J -0.2270726
2 B J 0.5860493
3 B K -0.5986129
4 C K 0.3135809
> results2
dim1 dim2 m1 m2
1 A J -0.2270726 -0.19037813
2 B J 0.5860493 -0.05385395
3 B K -0.5986129 0.29404095
4 C K 0.3135809 -0.26744010
5 A J -0.2270726 -0.19037813
6 B J 0.5860493 -0.05385395
7 B K -0.5986129 0.29404095
8 C K 0.3135809 -0.26744010
> results3
dim1 dim2 m1 m2 m3
1 A J -0.2270726 -0.19037813 -0.20448734
2 B J 0.5860493 -0.05385395 -0.11190857
3 B K -0.5986129 0.29404095 -0.27072101
4 C K 0.3135809 -0.26744010 -0.03184949
5 A J -0.2270726 -0.19037813 -0.20448734
6 B J 0.5860493 -0.05385395 -0.11190857
7 B K -0.5986129 0.29404095 -0.27072101
8 C K 0.3135809 -0.26744010 -0.03184949
9 A J -0.2270726 -0.19037813 -0.20448734
10 B J 0.5860493 -0.05385395 -0.11190857
11 B K -0.5986129 0.29404095 -0.27072101
12 C K 0.3135809 -0.26744010 -0.03184949
爲什麼有results2得到8行而不是4和results3得到12行而不是4?
感謝, 肖恩
+1謝謝... – Andrie 2011-04-15 13:36:02
正常工作1.4.1萬一別人剛剛升級的一切太:http://cran.r-project.org/src/contrib/Archive/plyr/plyr_1.4.1.tar.gz – 2011-04-17 01:20:43