Search code examples
rstdsummaryskewkurtosis

How to extend the 'summary' function to include sd, kurtosis and skew?


R's summary function works really well on a dataframe, giving, for example:

> summary(fred)
   sum.count          count              sum              value      
 Min.   : 1.000   Min.   :    1.0   Min.   :      1   Min.   : 0.00  
 1st Qu.: 1.000   1st Qu.:    6.0   1st Qu.:      7   1st Qu.:35.82  
 Median : 1.067   Median :    9.0   Median :     10   Median :42.17  
 Mean   : 1.238   Mean   :  497.1   Mean   :   6120   Mean   :43.44  
 3rd Qu.: 1.200   3rd Qu.:   35.0   3rd Qu.:     40   3rd Qu.:51.31  
 Max.   :40.687   Max.   :64425.0   Max.   :2621278   Max.   :75.95

What I'd like to do is modify the function so it also gives, after 'Mean', an entry for the standard deviation, the kurtosis and the skew.

What's the best way to do this? I've researched this a bit, and adding a function with a method doesn't work for me:

> summary.class <- function(x)
{
  return(sd(x))
}

The above is just ignored. I suppose that I need to understand how to define all classes to return.


Solution

  • How about using already existing solutions from the psych package?

    my.dat <- cbind(norm = rnorm(100), pois = rpois(n = 100, 10))
    
    library(psych)
    describe(my.dat)
    #    vars   n  mean   sd median trimmed  mad   min   max range  skew kurtosis   se
    # norm  1 100 -0.02 0.98  -0.09   -0.06 0.86 -3.25  2.81  6.06  0.13     0.74 0.10
    # pois  2 100  9.91 3.30  10.00    9.95 4.45  3.00 17.00 14.00 -0.07    -0.75 0.33