Search code examples
rr-markdown

Summary statistics table in R Markdown


How can I make pretty table of summary satistics in R Markdown with .pdf output?

I'm trying this solution as.data.frame(lapply(df, summary)), as seen here, but getting the following error: Error in dimnames(x) <- dnx : 'dimnames' applied to non-array.

Required output would be something like different statistics as the row names, and the different variables as the column names. The transpose is also good (and actually even preferable, but I believe it is harder). (1 variable with mean + standard deviation) Example:

Variable
Mean mu
SD sigma

Solution

  • A fast and complete summary can be obtained using basicStats from fBasics package:

    > library(fBasics)
    > basicStats(mtcars)|> round(2)
                   mpg    cyl     disp      hp   drat     wt   qsec    vs    am   gear  carb
    nobs         32.00  32.00    32.00   32.00  32.00  32.00  32.00 32.00 32.00  32.00 32.00
    NAs           0.00   0.00     0.00    0.00   0.00   0.00   0.00  0.00  0.00   0.00  0.00
    Minimum      10.40   4.00    71.10   52.00   2.76   1.51  14.50  0.00  0.00   3.00  1.00
    Maximum      33.90   8.00   472.00  335.00   4.93   5.42  22.90  1.00  1.00   5.00  8.00
    1. Quartile  15.43   4.00   120.83   96.50   3.08   2.58  16.89  0.00  0.00   3.00  2.00
    3. Quartile  22.80   8.00   326.00  180.00   3.92   3.61  18.90  1.00  1.00   4.00  4.00
    Mean         20.09   6.19   230.72  146.69   3.60   3.22  17.85  0.44  0.41   3.69  2.81
    Median       19.20   6.00   196.30  123.00   3.69   3.33  17.71  0.00  0.00   4.00  2.00
    Sum         642.90 198.00  7383.10 4694.00 115.09 102.95 571.16 14.00 13.00 118.00 90.00
    SE Mean       1.07   0.32    21.91   12.12   0.09   0.17   0.32  0.09  0.09   0.13  0.29
    LCL Mean     17.92   5.54   186.04  121.97   3.40   2.86  17.20  0.26  0.23   3.42  2.23
    UCL Mean     22.26   6.83   275.41  171.41   3.79   3.57  18.49  0.62  0.59   3.95  3.39
    Variance     36.32   3.19 15360.80 4700.87   0.29   0.96   3.19  0.25  0.25   0.54  2.61
    Stdev         6.03   1.79   123.94   68.56   0.53   0.98   1.79  0.50  0.50   0.74  1.62
    Skewness      0.61  -0.17     0.38    0.73   0.27   0.42   0.37  0.24  0.36   0.53  1.05
    Kurtosis     -0.37  -1.76    -1.21   -0.14  -0.71  -0.02   0.34 -2.00 -1.92  -1.07  1.26
    

    If you want the default summary from base R, then you can use sapply + summary:

    > sapply(mtcars, summary)|> round(2)
              mpg  cyl   disp     hp drat   wt  qsec   vs   am gear carb
    Min.    10.40 4.00  71.10  52.00 2.76 1.51 14.50 0.00 0.00 3.00 1.00
    1st Qu. 15.43 4.00 120.83  96.50 3.08 2.58 16.89 0.00 0.00 3.00 2.00
    Median  19.20 6.00 196.30 123.00 3.70 3.33 17.71 0.00 0.00 4.00 2.00
    Mean    20.09 6.19 230.72 146.69 3.60 3.22 17.85 0.44 0.41 3.69 2.81
    3rd Qu. 22.80 8.00 326.00 180.00 3.92 3.61 18.90 1.00 1.00 4.00 4.00
    Max.    33.90 8.00 472.00 335.00 4.93 5.42 22.90 1.00 1.00 5.00 8.00