freq {spicy} | R Documentation |
Frequency Table
Description
freq()
creates a frequency table for a variable or vector, with options for weighting, sorting, handling missing values, and calculating percentages.
Usage
freq(
data,
x = NULL,
weights = NULL,
digits = 1,
cum = FALSE,
total = TRUE,
exclude = NULL,
sort = "",
valid = TRUE,
na_val = NULL,
rescale_weights = FALSE,
info = TRUE,
labelled_levels = c("prefixed", "labels", "values"),
styled = TRUE,
show_empty_levels = FALSE,
...
)
Arguments
data |
A |
x |
A dataframe variable. |
weights |
A numeric vector of weights. Must be the same length as |
digits |
Numeric. Number of digits to be displayed for percentages. Default is |
cum |
Logical. If |
total |
Logical. If |
exclude |
Values to exclude (e.g., |
sort |
Sorting method for values:
|
valid |
Logical. If |
na_val |
Character or numeric. For factors, character or numeric vectors, values to be treated as |
rescale_weights |
Logical. If |
info |
Logical. If |
labelled_levels |
For
|
styled |
Logical. If |
show_empty_levels |
Logical. If |
... |
Additional arguments passed to |
Value
A formatted data.frame
containing unique values of x
, their frequencies (N
), percentages (%
), percentages of valid values (Valid%
), with a "Total" row.
If
cum = TRUE
, cumulative frequencies (%cum
andValid%cum
) are included.
Examples
data(iris)
data(mtcars)
freq(iris, Species)
iris |> freq(Species, cum = TRUE)
freq(mtcars, cyl, sort = "-", cum = TRUE)
freq(mtcars, gear, weights = mpg, rescale_weights = TRUE)
# With labelled variable
library(labelled)
df <- data.frame(
var1 = set_variable_labels(1:5, label = "Numeric Variable with Label"),
var2 = labelled(1:5, c("Low" = 1, "Medium" = 2, "High" = 3)),
var3 = set_variable_labels(
labelled(1:5, c("Bad" = 1, "Average" = 2, "Good" = 3)),
label = "Labelled Variable with Label"))
df |> freq(var2)
df |> freq(var2,labelled_levels = "l")
df |> freq(var2,labelled_levels = "v")
df |> freq(var3)
df |> freq(var3,labelled_levels = "v")
df |> freq(var3,labelled_levels = "l")