[R] aggregate function / custom column names?
Gabor Grothendieck
ggrothendieck at gmail.com
Thu Feb 11 18:43:34 CET 2010
Try this:
> aggregate(list(Max = df$value), df['id'], max)
id Max
1 11 2.610491
2 22 3.796836
3 33 6.562515
or if using value rather than Max is ok then just:
> aggregate(df['value'], df['id'], max)
id value
1 11 2.610491
2 22 3.796836
3 33 6.562515
On Thu, Feb 11, 2010 at 12:18 PM, Chuck White <chuckwhite8 at charter.net> wrote:
> 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)?
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
More information about the R-help
mailing list