Computes descriptive statistics by groups for a measure variable.

desc_statby(data, measure.var, grps, ci = 0.95)

Arguments

data

a data frame.

measure.var

the name of a column containing the variable to be summarized.

grps

a character vector containing grouping variables; e.g.: grps = c("grp1", "grp2")

ci

the percent range of the confidence interval (default is 0.95).

Value

A data frame containing descriptive statistics, such as:

  • length: the number of elements in each group

  • min: minimum

  • max: maximum

  • median: median

  • mean: mean

  • iqr: interquartile range

  • mad: median absolute deviation (see ?MAD)

  • sd: standard deviation of the sample

  • se: standard error of the mean. It's calculated as the sample standard deviation divided by the root of the sample size.

  • ci: confidence interval of the mean

  • range: the range = max - min

  • cv: coefficient of variation, sd/mean

  • var: variance, sd^2

Examples

# Load data
data("ToothGrowth")

# Descriptive statistics
res <- desc_statby(ToothGrowth, measure.var = "len",
   grps = c("dose", "supp"))
head(res[, 1:10])
#>   dose supp length  min  max median  mean   iqr     mad       sd
#> 1  0.5   OJ     10  8.2 21.5  12.25 13.23 6.475 4.29954 4.459709
#> 2  0.5   VC     10  4.2 11.5   7.15  7.98 4.950 3.55824 2.746634
#> 3  1.0   OJ     10 14.5 27.3  23.45 22.70 5.350 3.92889 3.910953
#> 4  1.0   VC     10 13.6 22.5  16.50 16.77 2.025 1.70499 2.515309
#> 5  2.0   OJ     10 22.4 30.9  25.95 26.06 2.500 2.07564 2.655058
#> 6  2.0   VC     10 18.5 33.9  25.95 26.14 5.425 4.59606 4.797731