如likert
函数 ( ?likert::likert
) 的文档中所述,data.frame 中的列items
应该是factor。然后,级别名称指定派生的李克特图中使用的响应标签。由于您的数据不可重现,请考虑以下人工示例:
library(likert)
set.seed(1)
df <- data.frame(Score = factor(sample(1:6, size = 100, replace = TRUE),
labels = c("Strongly Disagree", "Disagree", "Slightly Disagree", "Slightly Agree", "Agree", "Strongly Agree")))
(df_likert <- likert(items = df))
#> Item Strongly Disagree Disagree Slightly Disagree Slightly Agree Agree
#> 1 Score 19 18 12 15 15
#> Strongly Agree
#> 1 21
likert.bar.plot(df_likert)
编辑:对于表示data.frame中各个响应组的多个(例如数字)列,首先将列重新编码为因子,然后将likert
函数应用于重新编码的data.frame:
## initial data.frame of integers
df <- data.frame(
sapply(c("Q1", "Q2", "Q3"), function(x) sample(1:6, size = 100, replace = TRUE))
)
## recode each column as a factor
df_factor <- as.data.frame(
lapply(df, function(x) factor(x,
labels = c("Strongly Disagree", "Disagree", "Slightly Disagree",
"Slightly Agree", "Agree", "Strongly Agree"))
)
)
(df_likert <- likert(items = df_factor))
#> Item Strongly Disagree Disagree Slightly Disagree Slightly Agree Agree
#> 1 Q1 19 18 12 15 15
#> 2 Q2 19 16 19 18 15
#> 3 Q3 18 15 8 21 20
#> Strongly Agree
#> 1 21
#> 2 13
#> 3 18
likert.bar.plot(df_likert)