| freq | R Documentation |
Creates a frequency table for a vector or variable from a data frame, with options for weighting, sorting, handling labelled data, defining custom missing values, and displaying cumulative percentages.
When styled = TRUE, the function prints a spicy-formatted ASCII table
using print.spicy_freq_table() and spicy_print_table(); otherwise, it
returns a data.frame containing frequencies and proportions.
freq(
data,
x = NULL,
weights = NULL,
digits = 1,
valid = TRUE,
cum = FALSE,
sort = "",
na_val = NULL,
labelled_levels = c("prefixed", "labels", "values", "p", "l", "v"),
rescale = TRUE,
styled = TRUE,
...
)
data |
A |
x |
A variable from |
weights |
Optional numeric vector of weights (same length as |
digits |
Number of decimal digits to display for percentages (default: |
valid |
Logical. If |
cum |
Logical. If |
sort |
Sorting method for values:
|
na_val |
Vector of numeric or character values to be treated as missing ( For labelled variables (from haven or labelled), this argument must refer to the underlying coded values, not the visible labels. Example: x <- labelled(c(1, 2, 3, 1, 2, 3), c("Low" = 1, "Medium" = 2, "High" = 3))
freq(x, na_val = 1) # Treat all "Low" as missing
|
labelled_levels |
For
|
rescale |
Logical. If |
styled |
Logical. If |
... |
Additional arguments passed to |
This function is designed to mimic common frequency procedures from statistical software such as SPSS or Stata, while integrating the flexibility of R's data structures.
It automatically detects the type of input (vector, factor, or
labelled) and applies appropriate transformations, including:
Handling of labelled variables via labelled or haven
Optional recoding of specific values as missing (na_val)
Optional weighting with a rescaling mechanism
Support for cumulative percentages (cum = TRUE)
Multiple display modes for labels via labelled_levels
When weighting is applied (weights), the frequencies and percentages are
computed proportionally to the weights. The argument rescale = TRUE
normalizes weights so their sum equals the unweighted sample size.
A data.frame with columns:
value - unique values or factor levels
n - frequency count (weighted if applicable)
prop - proportion of total
valid_prop - proportion of valid responses (if valid = TRUE)
cum_prop, cum_valid_prop - cumulative percentages (if cum = TRUE)
If styled = TRUE, prints the formatted table to the console and returns it invisibly.
print.spicy_freq_table() for formatted printing.
spicy_print_table() for the underlying ASCII rendering engine.
library(labelled)
# Simple numeric vector
x <- c(1, 2, 2, 3, 3, 3, NA)
freq(x)
# Labelled variable (haven-style)
x_lbl <- labelled(
c(1, 2, 3, 1, 2, 3, 1, 2, NA),
labels = c("Low" = 1, "Medium" = 2, "High" = 3)
)
var_label(x_lbl) <- "Satisfaction level"
# Treat value 1 ("Low") as missing
freq(x_lbl, na_val = 1)
# Display only labels, add cumulative %
freq(x_lbl, labelled_levels = "labels", cum = TRUE)
# Display values only, sorted descending
freq(x_lbl, labelled_levels = "values", sort = "-")
# With weighting
df <- data.frame(
sexe = factor(c("Male", "Female", "Female", "Male", NA, "Female")),
poids = c(12, 8, 10, 15, 7, 9)
)
# Weighted frequencies (normalized)
freq(df, sexe, weights = poids, rescale = TRUE)
# Weighted frequencies (without rescaling)
freq(df, sexe, weights = poids, rescale = FALSE)
# Base R style, with weights and cumulative percentages
freq(df$sexe, weights = df$poids, cum = TRUE)
# Piped version (tidy syntax) and sort alphabetically descending ("name-")
df |> freq(sexe, sort = "name-")
# Non-styled return (for programmatic use)
f <- freq(df, sexe, styled = FALSE)
head(f)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.