desc_var: Describe a variable, optionally by groups

View source: R/desc_var.R

desc_varR Documentation

Describe a variable, optionally by groups

Description

Returns a dataframe with one row per group.

Usage

desc_var(y, group = NULL, data = NULL, digits = 3)

Arguments

y

A numeric vector of values, a column name (character string or unquoted) if data is provided, or a formula of the form y ~ x or y ~ x1 + x2 (for multiple grouping variables).

group

Optional grouping variable, if not provided computed for the full data. Ignored if y is a formula.

data

Optional data frame containing the variable(s).

digits

Number of decimal places to round to. Default is 3.

Details

The dependent variable ('y') must be numeric. If you pass an existing non-numeric variable (e.g., character, factor), 'desc_var()' will stop with a clear error indicating that the variable must be numeric.

Value

A data frame with one row per group (or one row if no group is specified) containing:

  • group: Group identifier

  • mean: Mean

  • sd: Standard deviation

  • se: Standard error

  • median: Median

  • min: Minimum

  • max: Maximum

  • mode: Most frequent value

  • freq_mode: Frequency of mode

  • mode2: 2nd most frequent value

  • freq_mode2: Frequency of 2nd mode

  • n.total: Number of observations

  • n.missing: Number of observations with missing (NA) values

  • n.unique: Number of unique values

Columns may also carry a "label" attribute, which provides a short human-readable description of each column.

Examples

# With grouping
df <- data.frame(y = rnorm(100), group = rep(c("A", "B"), 50))
desc_var(y, group, data = df)

# Without grouping (full dataset)
desc_var(y, data = df)

# Direct vectors
y <- rnorm(100)
group <- rep(c("A", "B"), 50)
desc_var(y, group)

# With custom decimal places
desc_var(y, group, data = df, digits = 2)

# Using formula syntax: y ~ x
desc_var(y ~ group, data = df)

# Using formula syntax with multiple grouping variables: y ~ x1 + x2
df2 <- data.frame(y = rnorm(200), x1 = rep(c("A", "B"), 100), x2 = rep(c("X", "Y"), each = 100))
desc_var(y ~ x1 + x2, data = df2)


statuser documentation built on April 25, 2026, 5:06 p.m.