This function provides descriptive statistics for a quantitative variable alone or seperately by groups. You can provide user-defined statistics.
dstats(data, x, ...)
data | data frame |
---|---|
x | numeric variable in data (unquoted) |
... | list of grouping variables |
statistics | statistics to calculate (any function that produces a 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 |
a data frame, where columns are grouping variables (optional) and statistics
# If no keyword arguments are provided, default values are used dstats(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 dstats(mtcars, mpg)#> n mean sd #> 1 32 20.09 6.03dstats(mtcars, mpg, am, gear, cyl, carb)#> am gear cyl carb n mean sd #> 1 0 3 4 1 1 21.50 NA #> 2 0 3 6 1 2 19.75 2.33 #> 3 0 3 8 2 4 17.15 2.09 #> 4 0 3 8 3 3 16.30 1.05 #> 5 0 3 8 4 5 12.62 2.09 #> 6 0 4 4 2 2 23.60 1.13 #> 7 0 4 6 4 2 18.50 0.99 #> 8 1 4 4 1 4 29.10 5.06 #> 9 1 4 4 2 2 25.90 6.36 #> 10 1 4 6 4 2 21.00 0.00 #> 11 1 5 4 2 2 28.20 3.11 #> 12 1 5 6 6 1 19.70 NA #> 13 1 5 8 4 1 15.80 NA #> 14 1 5 8 8 1 15.00 NA# You can input user-defined functions # my_n <- function(xs) length(xs) # dstats(mtcars, mpg, stats = c("my_n"), am, gear)