Aggregate function

Hello.

Recently I'm using aggregate function (stats package) and I noticed that it produces different results by different syntax I use.

Two type of syntax I used are:

  • aggregate(x~y, data, fun)

  • aggregate(data[...], by=list(...), fun)

Is it possible that aggregate function produces different results? Or different syntax could produce same results?

For the first type of syntax I noticed that produce different result when I use cbind to define variables to aggregate.

Thanks.

I do not see that behavior, as shown in the example below. Can you post an example?
By the way, aggregate is not part of the dplyr package, it is in the stats package.

set.seed(123)
DF <- data.frame(Name=sample(LETTERS[1:2],10,replace = TRUE),
                 Place=sample(LETTERS[3:4],10,replace = TRUE),
                 Value=rnorm(10))
aggregate(Value~Name+Place,data = DF,FUN = mean)
#>   Name Place      Value
#> 1    A     C  0.1142822
#> 2    B     C -0.0230071
#> 3    A     D  0.3572065
#> 4    B     D  0.4978505
aggregate(DF$Value,by=list(DF$Name,DF$Place),FUN=mean)
#>   Group.1 Group.2          x
#> 1       A       C  0.1142822
#> 2       B       C -0.0230071
#> 3       A       D  0.3572065
#> 4       B       D  0.4978505

Created on 2022-08-24 by the reprex package (v2.0.1)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.