Generate a simple set of summary statistics for a numeric vector or the numeric columns of a data.frame over various subsets. There are MANY other ways to do this but this function is suitable to my needs.

sumstats(data, ...)

# S3 method for numeric
sumstats(data, digits = 2, ...)

# S3 method for data.frame
sumstats(data, f = NULL, digits = 2, order = TRUE,
  ...)

Arguments

data

a data.frame or numeric vector.

...

additional arguments passed onto various methods.

digits

an integer to specify the number of decimal places desired for the resulting summary statistics. Passed directly to round().

f

a formula. See the examples and aggregate for details.

order

a logical value. If TRUE (default), the rows of the resulting data.frame will be first sorted alphabetically by the variables specified in the left-hand side of f then according to the permutations of the grouping factors specified in the right-hand side of f. If FALSE, the rows will be sorted only by permutations of the grouping factors.

Value

a data.frame in which each row represents a variable-grouping permutation.

Examples

# Numeric vector sumstats(mtcars$disp)
#> n mean sd min p25 p50 p75 max #> mtcars$disp 32 230.72 123.94 71.1 120.83 196.3 326 472
# All variables summarized and no grouping sumstats(mtcars)
#> variable n mean sd min p25 p50 p75 max #> 1 am 32 0.41 0.50 0.00 0.00 0.00 1.00 1.00 #> 2 carb 32 2.81 1.62 1.00 2.00 2.00 4.00 8.00 #> 3 cyl 32 6.19 1.79 4.00 4.00 6.00 8.00 8.00 #> 4 disp 32 230.72 123.94 71.10 120.83 196.30 326.00 472.00 #> 5 drat 32 3.60 0.53 2.76 3.08 3.70 3.92 4.93 #> 6 gear 32 3.69 0.74 3.00 3.00 4.00 4.00 5.00 #> 7 hp 32 146.69 68.56 52.00 96.50 123.00 180.00 335.00 #> 8 mpg 32 20.09 6.03 10.40 15.43 19.20 22.80 33.90 #> 9 qsec 32 17.85 1.79 14.50 16.89 17.71 18.90 22.90 #> 10 vs 32 0.44 0.50 0.00 0.00 0.00 1.00 1.00 #> 11 wt 32 3.22 0.98 1.51 2.58 3.33 3.61 5.42
sumstats(iris)
#> variable n mean sd min p25 p50 p75 max #> 1 Petal.Length 150 3.76 1.77 1.0 1.6 4.35 5.1 6.9 #> 2 Petal.Width 150 1.20 0.76 0.1 0.3 1.30 1.8 2.5 #> 3 Sepal.Length 150 5.84 0.83 4.3 5.1 5.80 6.4 7.9 #> 4 Sepal.Width 150 3.06 0.44 2.0 2.8 3.00 3.3 4.4
# Only 'Petal.Width' and 'Sepal.Width' summarize and no grouping sumstats(iris, cbind(Petal.Width, Sepal.Width) ~ NULL)
#> variable n mean sd min p25 p50 p75 max #> 1 Petal.Width 150 1.20 0.76 0.1 0.3 1.3 1.8 2.5 #> 2 Sepal.Width 150 3.06 0.44 2.0 2.8 3.0 3.3 4.4
# All variables summarized and grouped by 'Species' sumstats(iris, . ~ Species)
#> variable Species n mean sd min p25 p50 p75 max #> 1 Petal.Length setosa 50 1.46 0.17 1.0 1.40 1.50 1.58 1.9 #> 2 Petal.Length versicolor 50 1.33 0.20 1.0 1.20 1.30 1.50 1.8 #> 3 Petal.Length virginica 50 6.59 0.64 4.9 6.23 6.50 6.90 7.9 #> 4 Petal.Width setosa 50 3.43 0.38 2.3 3.20 3.40 3.68 4.4 #> 5 Petal.Width versicolor 50 4.26 0.47 3.0 4.00 4.35 4.60 5.1 #> 6 Petal.Width virginica 50 2.03 0.27 1.4 1.80 2.00 2.30 2.5 #> 7 Sepal.Length setosa 50 5.01 0.35 4.3 4.80 5.00 5.20 5.8 #> 8 Sepal.Length versicolor 50 2.77 0.31 2.0 2.52 2.80 3.00 3.4 #> 9 Sepal.Length virginica 50 5.55 0.55 4.5 5.10 5.55 5.88 6.9 #> 10 Sepal.Width setosa 50 0.25 0.11 0.1 0.20 0.20 0.30 0.6 #> 11 Sepal.Width versicolor 50 5.94 0.52 4.9 5.60 5.90 6.30 7.0 #> 12 Sepal.Width virginica 50 2.97 0.32 2.2 2.80 3.00 3.18 3.8
# 'mpg' and 'wt' summarized by 'cyl' and 'vs' sumstats(mtcars, cbind(mpg, wt) ~ cyl + vs)
#> variable cyl vs n mean sd min p25 p50 p75 max #> 1 mpg 4 0 1 26.00 NA 26.00 26.00 26.00 26.00 26.00 #> 2 mpg 4 1 10 2.30 0.60 1.51 1.86 2.26 2.70 3.19 #> 3 mpg 6 0 3 2.75 0.13 2.62 2.70 2.77 2.82 2.88 #> 4 mpg 6 1 4 19.12 1.63 17.80 18.03 18.65 19.75 21.40 #> 5 mpg 8 0 14 15.10 2.56 10.40 14.40 15.20 16.25 19.20 #> 6 wt 4 0 1 2.14 NA 2.14 2.14 2.14 2.14 2.14 #> 7 wt 4 1 10 26.73 4.75 21.40 22.80 25.85 30.40 33.90 #> 8 wt 6 0 3 20.57 0.75 19.70 20.35 21.00 21.00 21.00 #> 9 wt 6 1 4 3.39 0.12 3.21 3.38 3.44 3.45 3.46 #> 10 wt 8 0 14 4.00 0.76 3.17 3.53 3.75 4.01 5.42
sumstats(mtcars, cbind(mpg, wt) ~ cyl + vs, order = FALSE)
#> variable cyl vs n mean sd min p25 p50 p75 max #> 1 mpg 4 0 1 26.00 NA 26.00 26.00 26.00 26.00 26.00 #> 2 wt 6 0 3 20.57 0.75 19.70 20.35 21.00 21.00 21.00 #> 3 mpg 8 0 14 15.10 2.56 10.40 14.40 15.20 16.25 19.20 #> 4 wt 4 1 10 26.73 4.75 21.40 22.80 25.85 30.40 33.90 #> 5 mpg 6 1 4 19.12 1.63 17.80 18.03 18.65 19.75 21.40 #> 6 wt 4 0 1 2.14 NA 2.14 2.14 2.14 2.14 2.14 #> 7 mpg 6 0 3 2.75 0.13 2.62 2.70 2.77 2.82 2.88 #> 8 wt 8 0 14 4.00 0.76 3.17 3.53 3.75 4.01 5.42 #> 9 mpg 4 1 10 2.30 0.60 1.51 1.86 2.26 2.70 3.19 #> 10 wt 6 1 4 3.39 0.12 3.21 3.38 3.44 3.45 3.46