likertTable()
creates a summary table of frequencies and percentages for Likert scale items that can show breakdowns for
the data passed to it. The table that contains frequency and percent in each row and a column for each Likert scale response,
all items must have the same scale labels.
Arguments
- df
Required, a tibble or data frame of categorical/factor data that have the same scale labels.
- scale_labels
Required, a character vector of labels for the response scale of all the Likert items, must be in the desired order, e.g. if you have a 5 item scale of minimal to extensive it should look like this:
levels_min_ext <- c("Minimal", "Slight", "Moderate", "Good", "Extensive")
.- question_labels
Default is NULL. Takes in a named character vector to both supply labels the questions and sort the order of the questions.The named character vector should have the new labels as the "name" and the old labels as the "variable" sorted in the desired order of appearing in the table, first item will appear at the top of the table. See examples.
- str_width
Default is 20. The character length to wrap the question column. If question_labels are supplied and very long use this to keep the question column from being too large for the table.
Value
a flextable object with columns for question, each likert response item, and a total column for each items with the total n for that item. Colors are set to The Mark USA branding
Examples
data <- tibble::tribble(
~Organization, ~Source, ~Publish, ~Write, ~Research,
5L, 3L, 3L, 4L, 5L,
4L, 4L, 3L, 3L, 3L,
4L, 4L, 3L, 5L, 3L,
2L, 5L, 4L, 5L, 3L,
3L, 4L, 5L, 4L, 5L,
3L, 4L, 2L, 4L, 2L,
5L, 4L, 5L, 5L, 3L,
3L, 5L, 5L, 4L, 3L,
5L, 4L, 4L, 4L, 5L,
3L, 5L, 5L, 4L, 3L,
5L, 4L, 2L, 5L, 5L,
4L, 3L, 5L, 5L, 4L,
2L, 4L, 5L, 4L, 3L,
5L, 4L, 4L, 5L, 4L,
3L, 5L, 2L, 5L, 5L,
5L, 4L, 4L, 4L, 5L,
4L, 4L, 5L, 3L, 5L,
3L, 5L, 5L, 4L, 4L,
4L, 3L, 5L, 4L, 5L,
2L, 5L, 5L, 5L, 3L
)
# Scale labels for the Likert items:
levels_min_ext <- c("Minimal", "Slight", "Moderate", "Good", "Extensive")
# Question labels as a named vector with the naming structure
# like this: c("new label" = "original variable name"):
question_labels <- c("Publish a lot of high quality papers" = "Publish",
"Write a lot of research papers" = "Write",
"Research in a lab with faculty" = "Research",
"Organization of a large research project" = "Organization",
"Source work for a research paper" = "Source")
# Named Vector for recodeCat():
named_levels_min_ext <- c("Minimal" = "1", "Slight" = "2", "Moderate" = "3",
"Good" = "4", "Extensive" = "5")
# Recode the numeric to factor variables using the levels from levels_min_ext:
cat_items <- TheMarkUSA::recodeCat(data, named_levels_min_ext)
# Select the factor variables, and remove the prefix that recodeCat() added to the factor variables:
cat_items <- cat_items %>% dplyr::select(dplyr::where(is.factor)) %>%
dplyr::rename_with(., ~ stringr::str_remove(.,"cat_"))
# Another way to convert all to factor variables with levels if already character variables :
# cat_items <- data %>%
# mutate(across(everything(),~ factor(., levels = levels_min_ext)))
# Pass the factor variables and the levels to likertTable :
cat_items %>% likertTable(scale_labels = levels_min_ext, question_labels = question_labels)
Question
Minimal
Slight
Moderate
Good
Extensive
n
Publish a lot of
high quality papers
-
3 (15%)
3 (15%)
4 (20%)
10 (50%)
20
Write a lot of
research papers
-
-
2 (10%)
10 (50%)
8 (40%)
20
Research in a lab
with faculty
-
1 (5%)
8 (40%)
3 (15%)
8 (40%)
20
Organization of
a large research
project
-
3 (15%)
6 (30%)
5 (25%)
6 (30%)
20
Source work for a
research paper
-
-
3 (15%)
11 (55%)
6 (30%)
20
# Call likertTable without a question_labels:
cat_items %>% likertTable(scale_labels = levels_min_ext)
Question
Minimal
Slight
Moderate
Good
Extensive
n
Organization
-
3 (15%)
6 (30%)
5 (25%)
6 (30%)
20
Source
-
-
3 (15%)
11 (55%)
6 (30%)
20
Publish
-
3 (15%)
3 (15%)
4 (20%)
10 (50%)
20
Write
-
-
2 (10%)
10 (50%)
8 (40%)
20
Research
-
1 (5%)
8 (40%)
3 (15%)
8 (40%)
20