Get the means of sub groups of means in R

老子叫甜甜 提交于 2019-11-27 09:39:15

With base, using aggregate

> aggregate(WLN~GROUP+WORD, mean, data=df)
  GROUP WORD      WLN
1     1    1 3.333333
2     1    2 2.333333
3     2    3 1.333333
4     2    4 1.000000

where df is @Metrics' data.

Another alternative is using summaryBy from doBy package

> library(doBy)
> summaryBy(WLN~GROUP+WORD, FUN=mean, data=df)
  GROUP WORD WLN.mean
1     1    1 3.333333
2     1    2 2.333333
3     2    3 1.333333
4     2    4 1.000000

Assume df is your dataframe:

df<-structure(list(GROUP = c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L, 2L, 2L), WORD = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 
4L, 4L), WLN = c(4L, 3L, 3L, 2L, 2L, 3L, 1L, 1L, 2L, 1L, 1L, 
1L)), .Names = c("GROUP", "WORD", "WLN"), class = "data.frame", row.names = c(NA, 
-12L))

Plyr solution

install.packages("plyr")
library(plyr)
ddply(df,.(GROUP,WORD),summarize, meanwln=mean(WLN))
 GROUP WORD  meanwln
1     1    1 3.333333
2     1    2 2.333333
3     2    3 1.333333
4     2    4 1.000000

Data.table solution:

install.packages("data.table")
library(data.table)
df<-data.table(df)
setkey(df,GROUP,WORD)
df[,list(meanwln=mean(WLN)),by="GROUP,WORD"]

 GROUP WORD  meanwln
1:     1    1 3.333333
2:     1    2 2.333333
3:     2    3 1.333333
4:     2    4 1.000000

with base:

with(df,tapply(WLN,list(GROUP,WORD),mean))

Edit:

If you also want row- and colmeans for the table above, you could do something like this:

x <- with(df,tapply(WLN,list(GROUP,WORD),mean))
addmargins(x, margin = seq_along(dim(x)), FUN = mean, quiet = TRUE)

And now dplyr is even better...

require(dplyr)
tmp <- group_by(df, WORD)
df1 <- summarise(tmp, 
   count = n(), 
   mWLN = mean(WLN, na.rm = TRUE))
df1
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!