ddply:为什么这不起作用?

时间:2016-07-13 18:00:19

标签: r plyr

我在使用R时非常新,但看起来以下代码应该可行。为什么我没有得到我需要的统计数据?感谢。

> stats <- ddply(Y, "SECT", summarise, mean.EE = mean(EE), 
+                                      sd.EE = sd(EE),
+                                      mean.AA = mean(AA),
+                                      sd.AA = sd(AA),
+                                      mean.II = mean(II),
+                                      sd.II = sd(II))
> stats
  SECT mean.EE sd.EE mean.AA sd.AA mean.II sd.II
1   H1      NA    NA      NA    NA      NA    NA
2   H2      NA    NA      NA    NA      NA    NA
3   H3      NA    NA      NA    NA      NA    NA
> str(Y)
'data.frame':   537 obs. of  5 variables:
 $ WORD: chr  "a" "a" "a" "a" ...
 $ SECT: chr  "H1" "H1" "H1" "H1" ...
 $ EE  : num  2 2 2 2 2 ...
 $ AA  : num  1.38 1.38 1.38 1.38 1.38 ...
 $ II  : num  1 1 1 1 1 1 1 1 1 1 ...

PS - 我知道看起来每个变量(EE,AA,II)都没有变化,但这仅仅是因为它们已被排序......

1 个答案:

答案 0 :(得分:0)

也可以尝试使用dplyr

# install.packages('dplyr')
library(dplyr)

meanna <- function(x) mean(x, na.rm = T)
sdna <- function(x) sd(x, na.rm = T)

Y %>%
  group_by(SECT) %>%
  summarise_each(funs(meanna, sdna), -WORD)