Skip to content
Prev 95714 / 398500 Next

Summary Statistics for data.frame

On 7/8/2006 3:44 PM, justin rapp wrote:
I think the quickest is

by(mydf, mydf$Year, summary)

but this won't give you the variance.  You'll need your own little 
function to calculate mean and variance, e.g.

mysummary <- function(df) apply(df, 2,
                function(x) c(mean=mean(x), variance=var(x)))

by(mydf, mydf$Year, mysummary)

If you don't like the format of the output, you can play around with the 
mysummary function.  It will be applied to each subset of the 
data.frame, and the results will be put together into a list with one 
entry per level of mydf$Year.


Duncan