aggregate.data.frame(x, by, FUN, ..., simplify = TRUE)
x | a data frame. If x is not a data frame, it is converted to one using the data.frame function. A 0-row data frame is not allowed. |
by | a list of grouping vectors, each as long as a column of x. The list elements should be named so that the result can use those names for its corresponding columns. |
FUN | a function that can be applied to any column of x and that returns a single value. |
... | any other arguments are passed to FUN. |
simplify | a logical flag. Determines how to deal with the result of the applied FUN. If TRUE, the result should be simplified to a vector or matrix if possible. |
aggregate(Sdatasets::iris[,1:4], list(Sdatasets::iris[,5]), mean) ## Group.1 Sepal.Length Sepal.Width Petal.Length Petal.Width ## 1 setosa 5.006 3.428 1.462 0.246 ## 2 versicolor 5.936 2.770 4.260 1.326 ## 3 virginica 6.588 2.974 5.552 2.026da <- data.frame(col1=c(1,2,3,4,5), col2=c(6,7,8,9,10)) by <- list(c("a", "b", "c", "d", "e")) aggregate(da, by, mean) ## Group.1 col1 col2 ## 1 a 1 6 ## 2 b 2 7 ## 3 c 3 8 ## 4 d 4 9 ## 5 e 5 10
by <- list(c("a", "b", "c", "a", "b")) aggregate(da, by, mean) ## Group.1 col1 col2 ## 1 a 2.5 7.5 ## 2 b 3.5 8.5 ## 3 c 3.0 8.0