This function provides descriptive statistics for a quantitative variable alone or separately by groups. Any function that returns a single numeric value can bue used.

qstats(data, x, ..., stats = c("n", "mean", "sd"), na.rm = TRUE, digits = 2)

Arguments

data

data frame

x

numeric variable in data (unquoted)

...

list of grouping variables

stats

statistics to calculate (any function that produces a numeric value), Default: c("n", "mean", "sd")

na.rm

if TRUE, delete cases with missing values on x and or grouping variables, Default: TRUE

digits

number of decimal digits to print, Default: 2

Value

a data frame, where columns are grouping variables (optional) and statistics

Examples

# If no keyword arguments are provided, default values are used
qstats(mtcars, mpg, am, gear)
#>   am gear  n  mean   sd
#> 1  0    3 15 16.11 3.37
#> 2  0    4  4 21.05 3.07
#> 3  1    4  8 26.27 5.41
#> 4  1    5  5 21.38 6.66

# You can supply as many (or no) grouping variables as needed
qstats(mtcars, mpg)
#>    n  mean   sd
#> 1 32 20.09 6.03

qstats(mtcars, mpg, am, cyl)
#>   am cyl  n  mean   sd
#> 1  0   4  3 22.90 1.45
#> 2  0   6  4 19.12 1.63
#> 3  0   8 12 15.05 2.77
#> 4  1   4  8 28.08 4.48
#> 5  1   6  3 20.57 0.75
#> 6  1   8  2 15.40 0.57

# You can specify your own functions (e.g., median,
# median absolute deviation, minimum, maximum))
qstats(mtcars, mpg, am, gear,
       stats = c("median", "mad", "min", "max"))
#>   am gear median  mad  min  max
#> 1  0    3  15.50 3.26 10.4 21.5
#> 2  0    4  21.00 3.71 17.8 24.4
#> 3  1    4  25.05 6.00 21.0 33.9
#> 4  1    5  19.70 6.97 15.0 30.4