[R] aggregate data.frame based on column class

Martin Batholdy batholdy at googlemail.com
Fri Jan 11 16:07:41 CET 2013


When using the aggregate function to aggregate a data.frame by one or more grouping variables I often have the problem, that I want the mean for some numeric variables but the unique value for factor variables.

So for example in this data-frame:

data <- data.frame(x = rnorm(10,1,2), group = c(rep(1,5), rep(2,5)), gender =c(rep('m',5), rep('f',5)))
aggregate(data, by=list(data$group), FUN=mean)

I would like to have 'm' and 'f' in the third column, not NA.

I see the problem, that it could happen that there is no unique factor level in a group –
but is there an alternative function who at least tries what I am aiming at?

That is;

"aggregate the data.frame by a list of grouping variables,
for numeric variables compute the mean,
for factor variables return the unique factor value"


More information about the R-help mailing list