Compute Summary Statistics of Subsets in R Programming – aggregate() function

In R programming, aggregate() function is used to compute the summary statistics of the split data. It takes the data frame or time series analysis object.

Syntax: aggregate(x, by, FUN)

Parameters:
x: specifies R object
by: specifies list of grouping elements
FUN: specifies function to compute the statistical summary

To know about more optional parameters, use below command in console:

help("aggregate")

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

# Using state.x77 and state.region dataset
# Compute the mean of the states in state.x77
# grouped by state.region variables
  
aggregate(state.x77, list(region = state.region), mean)

chevron_right


Output:

         region Population   Income Illiteracy Life Exp    Murder  HS Grad    Frost      Area
1     Northeast   5495.111 4570.222   1.000000 71.26444  4.722222 53.96667 132.7778  18141.00
2         South   4208.125 4011.938   1.737500 69.70625 10.581250 44.34375  64.6250  54605.12
3 North Central   4803.000 4611.083   0.700000 71.76667  5.275000 54.51667 138.8333  62652.00
4          West   2915.308 4702.615   1.023077 71.23462  7.215385 62.00000 102.1538 134463.00

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Using mtcars dataset
# Compute the mean of all columns in mtcars
# grouped by gears
aggregate(mtcars, list(mtcars$gears), mean)

chevron_right


Output:

    Group.1   mpg      cyl     disp       hp     drat       wt   qsec        vs     am gear     carb
1       3 16.10667 7.466667 326.3000 176.1333 3.132667 3.892600 17.692 0.2000000 0.0000000    3 2.666667
2       4 24.53333 4.666667 123.0167  89.5000 4.043333 2.616667 18.965 0.8333333 0.6666667    4 2.333333
3       5 21.38000 6.000000 202.4800 195.6000 3.916000 2.632600 15.640 0.2000000 1.0000000    5 4.400000
My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.