[Experimental]

tbl_likert() creates a summary of Likert scales following the gtsummary structure.

add_n() adds a column to the table with the total number of observations.

add_continuous_stat() converts Likert scales into a numeric score and computes continuous statistics based on this score.

tbl_likert(
  data,
  label = NULL,
  statistic = NULL,
  digits = NULL,
  include = everything(),
  sort = c("default", "ascending", "descending")
)

# S3 method for tbl_likert
add_n(
  x,
  statistic = "{n}",
  col_label = "**N**",
  footnote = FALSE,
  last = FALSE,
  ...
)

add_continuous_stat(x, ...)

# S3 method for tbl_likert
add_continuous_stat(
  x,
  statistic = "{mean}",
  digits = NULL,
  col_label = NULL,
  footnote = FALSE,
  last = TRUE,
  score_values = NULL,
  stat_col_name = NULL,
  ...
)

Arguments

data

A data frame

label

List of formulas specifying variables labels, e.g. list(age ~ "Age", stage ~ "Path T Stage"). If a variable's label is not specified here, the label attribute (attr(data$age, "label")) is used. If attribute label is NULL, the variable name will be used.

statistic

String or formula indicating the statistic to be reported. Default is the mean score. Other possible continuous statistics are described in gtsummary::tbl_summary() help page, section statistic argument.

digits

Formula or list of formulas indicating how to display the computed statistics, see gtsummary::tbl_summary() help page

include

variables to include in the summary table. Default is everything()

sort

Sort table based on mean scores? Must be one of c("default", "ascending", "descending")

x

Object with class tbl_likert from the tbl_likert() function

col_label

String indicating the column label. Default is generated from statistic.

footnote

Logical argument indicating whether to print a footnote clarifying the statistics presented. Default is FALSE

last

Logical indicator to include the new column last in table. Default is TRUE

...

not used

score_values

Vector indicating the numeric value of each factor level. Default is 1:n where n indicates the number of levels.

stat_col_name

Optional string indicating the name of the new column added to x$table_body

Example Output

Example 1

Examples

library(dplyr)
#> 
#> Attaching package: ‘dplyr’
#> The following objects are masked from ‘package:stats’:
#> 
#>     filter, lag
#> The following objects are masked from ‘package:base’:
#> 
#>     intersect, setdiff, setequal, union
set.seed(1123)
likert_lvls <- c("Never", "Sometimes",  "Often", "Always")

df <-
  tibble::tibble(
    Q1 = sample(likert_lvls, size = 100, replace = TRUE),
    Q2 = sample(likert_lvls, size = 100, replace = TRUE)
  ) %>%
  mutate(across(c(Q1, Q2), ~factor(., levels = likert_lvls)))

tbl_likert_ex1 <-
  tbl_likert(df) %>%
  add_n() %>%
  add_continuous_stat(statistic = "{mean}") %>%
  add_continuous_stat(statistic = "{sd}")