smalldat <- data.frame(group1 = rep(1:2, c(5,5)),
group2 = rep(c("a","b"), 5),
x = rnorm(10))
smalldat
# group1 group2 x
# 1 1 a -1.2173399
# 2 1 b 0.2601609
# 3 1 a -1.9955389
# 4 1 b -0.7949134
# 5 1 a 0.9655160
# 6 2 b -1.2307946
# 7 2 a 0.3562118
# 8 2 b 0.7674343
# 9 2 a -0.2472418
# 10 2 b -1.2653220
a<-group_by(smalldat,group1)
summarize(a,mm=mean(x))
# mm
# 1 -0.1690133
那么,为什么我得到所有x的平均值,而不是1和2的平均值? 谢谢
答案 0 :(得分:1)
你需要打破管道。
smalldat %>% group_by(group1) %>% summarize(mm = mean(x))
# Source: local data frame [2 x 2]
#
# group1 mm
# (int) (dbl)
# 1 1 -0.5564231
# 2 2 -0.3239425
(必备data.table
插件:我觉得这更具可读性):
library(data.table); setDT(smalldat)
smalldat[ , mean(x), by = group1]
#or, named:
smalldat[ , .(mean(x)), by = group1]
答案 1 :(得分:0)
作为替代方案,我们可以使用aggregate
base R
aggregate(x~group1, smalldat, mean)
# group1 x
#1 1 0.2487354
#2 2 0.2275124