Skip to contents

tbl_likert() creates a table of ordered categorical variables in a wide format: one row per variable and one column per level, which suits Likert scales. All variables must be factors with identical levels, and the denominator of the percentages is each variable's number of non-missing observations. add_n() works on the table with its tbl_summary() interface.

Usage

tbl_likert(
  data,
  statistic = ~"{n} ({p}%)",
  label = NULL,
  digits = NULL,
  include = everything(),
  sort = c("ascending", "descending")
)

Arguments

data

(data.frame)
A data frame.

statistic

(formula-list)
The summary statistic of every cell, a single string in which {n}, {N} and {p} are available. Default is everything() ~ "{n} ({p}%)".

label

(formula-list)
Variable labels, e.g. list(age = "Age, years"). The default for each variable is its label attribute, or the column name when there is none.

digits

(formula-list)
How the statistics are rounded: integers or formatting functions. When not specified, the defaults come from assign_summary_digits().

include

(selector)
Variables to include. Default is everything().

sort

(string)
Present the factor levels in "ascending" order (the default) or "descending", which reverses the columns.

Value

A table of class c("tbl_likert", "ltsummary").

Examples

set.seed(11235)
levels <- c("Strongly Disagree", "Disagree", "Agree", "Strongly Agree")
df_likert <- data.frame(
  recommend_friend = sample(levels, size = 20, replace = TRUE) |> factor(levels = levels),
  regret_purchase = sample(levels, size = 20, replace = TRUE) |> factor(levels = levels)
)

# Example 1 ----------------------------------
df_likert |>
  tbl_likert(include = c(recommend_friend, regret_purchase)) |>
  add_n()