aggregate() naming -- bug or feature

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

aggregate() naming -- bug or feature

rpruim
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 = stats::sd(x))}
aggregate(iris$Sepal.Length, by = list(iris$Species), FUN = foo)
#>      Group.1     x
#> 1     setosa 5.006
#> 2 versicolor 5.936
#> 3  virginica 6.588
aggregate(iris$Sepal.Length, by = list(iris$Species), FUN = bar)
#>      Group.1    x.mean      x.sd
#> 1     setosa 5.0060000 0.3524897
#> 2 versicolor 5.9360000 0.5161711
#> 3  virginica 6.5880000 0.6358796

—rjp


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: aggregate() naming -- bug or feature

Rui Barradas
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     setosa        5.006
#2 versicolor        5.936
#3  virginica        6.588


Hope this helps,

Rui Barradas

On 3/23/2018 1:29 PM, Randall Pruim wrote:

> 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 = stats::sd(x))}
> aggregate(iris$Sepal.Length, by = list(iris$Species), FUN = foo)
> #>      Group.1     x
> #> 1     setosa 5.006
> #> 2 versicolor 5.936
> #> 3  virginica 6.588
> aggregate(iris$Sepal.Length, by = list(iris$Species), FUN = bar)
> #>      Group.1    x.mean      x.sd
> #> 1     setosa 5.0060000 0.3524897
> #> 2 versicolor 5.9360000 0.5161711
> #> 3  virginica 6.5880000 0.6358796
>
> —rjp
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: aggregate() naming -- bug or feature

Ista Zahn
On Fri, Mar 23, 2018 at 6:43 PM, Rui Barradas <[hidden email]> 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
# 1     setosa        5.006
# 2 versicolor        5.936
# 3  virginica        6.588

If fact, this

> is even better than those names assigned by bar.
>
>
> aggregate(Sepal.Length ~ Species, data = iris, FUN = foo)
> #     Species Sepal.Length
> #1     setosa        5.006
> #2 versicolor        5.936
> #3  virginica        6.588
>
>
> Hope this helps,
>
> Rui Barradas
>
>
> On 3/23/2018 1:29 PM, Randall Pruim wrote:
>>
>> 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 = stats::sd(x))}
>> aggregate(iris$Sepal.Length, by = list(iris$Species), FUN = foo)
>> #>      Group.1     x
>> #> 1     setosa 5.006
>> #> 2 versicolor 5.936
>> #> 3  virginica 6.588
>> aggregate(iris$Sepal.Length, by = list(iris$Species), FUN = bar)
>> #>      Group.1    x.mean      x.sd
>> #> 1     setosa 5.0060000 0.3524897
>> #> 2 versicolor 5.9360000 0.5161711
>> #> 3  virginica 6.5880000 0.6358796
>>
>> —rjp
>>
>>
>>         [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: aggregate() naming -- bug or feature

rpruim

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.frame().

For now, I’ve written by own local version of aggregate() with a few tweaks to retain the names I want.

But my question remains: Is this a bug or a feature?

—rjp


On Mar 23, 2018, at 6:57 PM, Ista Zahn <[hidden email]<mailto:[hidden email]>> wrote:

On Fri, Mar 23, 2018 at 6:43 PM, Rui Barradas <[hidden email]<mailto:[hidden email]>> 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
# 1     setosa        5.006
# 2 versicolor        5.936
# 3  virginica        6.588

If fact, this
is even better than those names assigned by bar.


aggregate(Sepal.Length ~ Species, data = iris, FUN = foo)
#     Species Sepal.Length
#1     setosa        5.006
#2 versicolor        5.936
#3  virginica        6.588


Hope this helps,

Rui Barradas


On 3/23/2018 1:29 PM, Randall Pruim wrote:

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 = stats::sd(x))}
aggregate(iris$Sepal.Length, by = list(iris$Species), FUN = foo)
#>      Group.1     x
#> 1     setosa 5.006
#> 2 versicolor 5.936
#> 3  virginica 6.588
aggregate(iris$Sepal.Length, by = list(iris$Species), FUN = bar)
#>      Group.1    x.mean      x.sd
#> 1     setosa 5.0060000 0.3524897
#> 2 versicolor 5.9360000 0.5161711
#> 3  virginica 6.5880000 0.6358796

—rjp


       [[alternative HTML version deleted]]

______________________________________________
[hidden email]<mailto:[hidden email]> mailing list
https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Ddevel&d=DwIFaQ&c=4rZ6NPIETe-LE5i2KBR4rw&r=S6U-baLhvGcJ7iUQX_KZ6K2om1TTOeUI_-mjRpTrm00&m=wR-DoggMzZ5fX3PJlgbTQe2njoPJ03CTiimaCc_OHe0&s=rehyeJZBteb4wYmKFPvE74AzY4Nm__6Cm4h2q4xfXnk&e=


______________________________________________
[hidden email]<mailto:[hidden email]> mailing list
https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Ddevel&d=DwIFaQ&c=4rZ6NPIETe-LE5i2KBR4rw&r=S6U-baLhvGcJ7iUQX_KZ6K2om1TTOeUI_-mjRpTrm00&m=wR-DoggMzZ5fX3PJlgbTQe2njoPJ03CTiimaCc_OHe0&s=rehyeJZBteb4wYmKFPvE74AzY4Nm__6Cm4h2q4xfXnk&e=


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel