Learn R Programming

statuser (version 0.1.9)

desc_var: 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)

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

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.

Examples

Run this code
# 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)

Run the code above in your browser using DataLab