Question

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))
Était-ce utile?

La solution 2

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

Autres conseils

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
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top