Using aggregate to find a function (mean) for “all other”

末鹿安然 提交于 2019-12-25 02:44:10

问题


I've been playing around with aggregate and plyr -- and searched SO -- but can't seem figure this out.

I'm trying to get the mean for "all other" for a given variable. So, I can use aggregate to get the mean of var for each one of the variables in group -- yes, the variables are characters and have to be escaped :-(

What I'd like to do is get:

  1. the mean for just - , and then separately
  2. the mean for all other variables (/ * #) or not-- combined.

So in the example data below, the mean for - is 2 and the mean for "all other" is 4.

The real data sets are much longer (1000's of lines), more complex, and variable, so it wouldn't make sense to type the "all other" in separately -- I am thinking something like ~. would be the way to go.

group     var
-         4
-         0
-         2
/         2
/         5
*         3
*         7
*         3
#         4

structure(list(group = structure(c(1L, 1L, 1L, 3L, 3L, 2L, 2L, 
2L), .Label = c("-", "*", "/"), class = "factor"), var = c(4L, 
0L, 2L, 2L, 5L, 3L, 7L, 3L)), .Names = c("group", "var"), class = "data.frame", row.names = c(NA, 
-8L))

回答1:


This will return the mean of just the group "-"

mean(df[which(df$group=="-"),]$var)
[1] 2

This will return the mean of everything other than "-"

mean(df[which(df$group!="-"),]$var)
[1] 4

And this will return the mean of each group other than "-"

df1<-df[which(df$group!="-"),]
ddply(df1,.(group),summarise,mean=mean(var))
  group     mean
1     * 4.333333
2     / 3.500000
3     # 4.000000



回答2:


1) aggregate.formula Try this:

aggregate(var ~ group, transform(DF, group = ifelse(group == "-", "-", "other")), mean)

giving:

  group var
1     -   2
2 other   4

2) aggregate.data.frame or it could be written like this:

group <- ifelse(DF$group == "-", "-", "other") 
aggregate(DF["var"], data.frame(group), mean)

giving:

  group var
1     -   2
2 other   4


来源:https://stackoverflow.com/questions/22270649/using-aggregate-to-find-a-function-mean-for-all-other

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!