[R] aggregate function / custom column names?

Chuck White chuckwhite8 at charter.net
Thu Feb 11 18:18:19 CET 2010


This question is about column names returned by the aggregate function. Consider the following example

df <- data.frame(
   id = c(rep('11',30),rep('22',30),rep('33',30)),
   value = c(rnorm(30,2,0.5), rnorm(30,3,0.5), rnorm(30,6,0.5))
)

aggregate(df[,c("value"),drop=FALSE], by=list(id=df$id), max)
output:
  id    value
1 11 2.693528
2 22 3.868400
3 33 6.942519

aggregate(df$value, by=list(id=df$id), max)
output:
  id        x
1 11 2.693528
2 22 3.868400
3 33 6.942519

(YMMV on output values since data is randomly generated)

I would like to be able to name the output column as max.value.  I realize I can add the following statement:
colnames(df)[match("value",colnames(df))] <- "max.value"

Is there a way of having aggregate return computed column names which can be specified when calling the function (i.e. aggregate)?



More information about the R-help mailing list