Compute Column-by-Column Summaries of Groups of Observations

DESCRIPTION:

Given a data frame, one or more grouping vectors, and a summary function, returns a data frame containing results of summaries of each column broken down by group.

USAGE:

aggregate.data.frame(x, by, FUN, ...) 

REQUIRED ARGUMENTS:

x
a data frame. If x is not a data frame, it is converted to one using the data.frame function.
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.

OPTIONAL ARGUMENTS:

...
any other arguments are passed to FUN.

VALUE:

a data frame with a column for each column in by and x. The columns arising from by contain each unique combination of values in the grouping vectors (excluding combinations not seen in the data). These columns have data class "factor". The columns arising from x contain the value of FUN applied to the partitions induced by the grouping vectors on each column of x.

DETAILS:

This is the "data.frame" method for the generic function aggregate.

NOTE:

If x has columns of various types, it might be difficult to find a summary function that works on all columns. Instead, it may be easier to use aggregate.data.frame on only certain columns of x.

SEE ALSO:

, , , .

EXAMPLES:

aggregate(fuel.frame[,1:4], list(Type=fuel.frame$Type), mean)
# Produces:
#            Type   Weight     Disp.  Mileage     Fuel 
# Compact Compact 2821.000 140.40000 24.13333 4.167655
#   Large   Large 3676.667 279.33333 20.33333 4.967794
#  Medium  Medium 3195.769 175.84615 21.76923 4.601413
#   Small   Small 2257.692  97.30769 31.00000 3.273380
#  Sporty  Sporty 2798.889 164.11111 26.00000 3.957606
#     Van     Van 3517.143 164.42857 18.85714 5.313283