This function provides a summary of a dataset, including both numeric and
non-numeric variables. For numeric variables, it calculates basic descriptive
statistics such as minimum, maximum, median, mean, and count of non-missing
values. Additionally, users can pass custom functions via the fn argument to
compute additional statistics for numeric variables. For non-numeric variables,
it provides frequency counts and proportions for each unique value.
Arguments
- data
A data frame containing the dataset to be summarized.
- ...
(Optional) Columns to include in the summary. If no column is specified, all columns in the data will be included.
- fn
A list of functions to apply to numeric variables. Each function must accept
xas a vector of numeric values and return a single value or a named vector. Additional arguments for these functions can be specified as a list. For example:fn = list('sum' = list(na.rm = TRUE), 'sd').- by_group
Logical, default
TRUE. WhenTRUEand both categorical and numeric variables are selected, each numeric variable is summarised within the groups defined by the categorical variable(s). WhenFALSE, numeric and categorical variables are summarised independently and stacked into a single table. If only one variable type is present,by_grouphas no effect.
Value
A tibble whose shape depends on by_group.
With by_group = TRUE (and at least one categorical and one numeric variable),
the data are grouped by the selected categorical variable(s) and each numeric
variable is summarised within every group. The result has one row per numeric
variable and group combination, with columns:
VariableName of the numeric variable being summarised.
- grouping column(s)
One column per selected categorical variable, each holding the group value.
NNumber of non-missing values of
Variablein the group.Min,Max,Median,MeanDescriptive statistics of
Variablewithin the group.CI Left,CI RightLower and upper bounds of the 95% t-based confidence interval for the group mean (see
ct_ci()).
With by_group = FALSE (also used as a fallback when only numeric or only
categorical variables are selected), numeric and categorical summaries are
stacked into one tibble with one row per numeric variable and one row per
distinct value of each categorical variable; columns that do not apply to a row
are NA:
VariableName of the summarised column.
GroupFor a categorical variable, the distinct value described;
NAfor numeric variables.PropFor a categorical variable, the percentage of its non-missing records falling in
Group;NAfor numeric variables.NNon-missing count: values for a numeric variable, or records in
Groupfor a categorical variable.Min,Max,Median,MeanNumeric statistics;
NAfor categorical variables.CI Left,CI Right95% t-based confidence interval bounds for the mean (see
ct_ci());NAfor categorical variables.
In both modes, supplying fn appends one extra column per named function,
holding that statistic for each numeric variable (or group).
Examples
df <- data.frame(x = c(1:3, NA),
y = c(3:4, NA, NA),
z = c("A", "A", "B", "A"))
# Numeric variables summarised within each group of the categorical variable
ct_describe_df(df, y, x, z)
#> # A tibble: 4 × 9
#> Variable z N Min Max Median Mean `CI Left` `CI Right`
#> <chr> <chr> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 y A 2 3 4 3.5 3.5 -2.85 9.85
#> 2 y B 0 NA NA NA NaN NA NA
#> 3 x A 2 1 2 1.5 1.5 -4.85 7.85
#> 4 x B 1 3 3 3 3 NA NA
# Summarise every variable independently
ct_describe_df(df, y, x, z, by_group = FALSE)
#> # A tibble: 4 × 10
#> Group Prop N Variable Mean Min Max Median `CI Left` `CI Right`
#> <chr> <dbl> <int> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 NA NA 2 y 3.5 3 4 3.5 -2.85 9.85
#> 2 NA NA 3 x 2 1 3 2 -0.484 4.48
#> 3 A 75 3 z NA NA NA NA NA NA
#> 4 B 25 1 z NA NA NA NA NA NA
# Add custom statistics for the numeric variables
ct_describe_df(df, y, x, z,
fn = list('sum' = list(na.rm = TRUE), 'sd' = list(na.rm = TRUE)))
#> # A tibble: 4 × 11
#> Variable z N Min Max Median Mean `CI Left` `CI Right` sum
#> <chr> <chr> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <int>
#> 1 y A 2 3 4 3.5 3.5 -2.85 9.85 7
#> 2 y B 0 NA NA NA NaN NA NA 0
#> 3 x A 2 1 2 1.5 1.5 -4.85 7.85 3
#> 4 x B 1 3 3 3 3 NA NA 3
#> # ℹ 1 more variable: sd <dbl>
