Be aware that the object that aggregate returns with bar() is more complicated
than you think.
str(aggregate(iris$Sepal.Length, by = list(iris$Species), FUN = bar))
'data.frame': 3 obs. of 2 variables:
$ Group.1: Factor w/ 3 levels "setosa","versicolor",..: 1 2 3
$ x : num [1:3, 1:2] 5.
Thanks.
I’m aware of the other syntax. My example was just to illustrate the issue
minimally, not to indicate how I am using aggregate(). In my application,
aggregate() will be called within another function, and the information passed
to aggregate() is columns of a matrix returned by model.
On Fri, Mar 23, 2018 at 6:43 PM, Rui Barradas wrote:
> Hello,
>
> Not exactly an answer but here it goes.
> If you use the formula interface the names will be retained.
Also if you pass named arguments:
aggregate(iris["Sepal.Length"], by = iris["Species"], FUN = foo)
# Species Sepal.Length
Hello,
Not exactly an answer but here it goes.
If you use the formula interface the names will be retained. If fact,
this is even better than those names assigned by bar.
aggregate(Sepal.Length ~ Species, data = iris, FUN = foo)
# Species Sepal.Length
#1 setosa5.006
#2 versico
In the examples below, the first loses the name attached by foo(), the second
retains names attached by bar(). Is this an intentional difference? I’d
prefer that the names be retained in both cases.
foo <- function(x) { c(mean = base::mean(x)) }
bar <- function(x) { c(mean = base::mean(x), sd