使用 lapply 和 factor() 解析调查数据的最佳方法是什么?

What is the best way to use lapply with factor() to parse survey data?

我正在尝试使用函数和 lapply() 循环简化我的 R 代码,但在尝试将提取调查数据的代码块转换为一组聚类李克特式问题时遇到了困难。这是原始的工作代码:

require(likert)
require(tidyverse)

# Generate a data compliments of dput
q25_data <- structure(list(Q25_self_and_family = c(4, 2, 3, 3, 5, 3), Q25_local_area = c(3, 
3, 3, 3, 5, 3), Q25_uk = c(4, 3, 3, 3, 5, 2), Q25_outside_uk = c(4, 
4, 3, 3, 5, 4)), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame"))

# Set up levels text for question responses
q25_levels <- c("not at all serious", "somewhat serious", "moderately serious", "Somewhat Agree", "extremely serious")
q25_data$Q25_self_and_family <- factor(q25_data$Q25_self_and_family, ordered = TRUE, levels = c("1", "2", "3", "4", "5"))
q25_data$Q25_local_area <- factor(q25_data$Q25_local_area, ordered = TRUE, levels = c("1", "2", "3", "4", "5"))
q25_data$Q25_uk <- factor(q25_data$Q25_uk, ordered = TRUE, levels = c("1", "2", "3", "4", "5"))
q25_data$Q25_outside_uk <- factor(q25_data$Q25_outside_uk, ordered = TRUE, levels = c("1", "2", "3", "4", "5"))
# Change factor names to match level text
q25_data$Q25_self_and_family <- fct_recode(q25_data$Q25_self_and_family, "not at all serious" = "1", "somewhat serious" = "2", "moderately serious" = "3", "very serious" = "4", "extremely serious" = "5")
q25_data$Q25_local_area <- fct_recode(q25_data$Q25_local_area, "not at all serious" = "1", "somewhat serious" = "2", "moderately serious" = "3", "very serious" = "4", "extremely serious" = "5")
q25_data$Q25_uk <- fct_recode(q25_data$Q25_uk, "not at all serious" = "1", "somewhat serious" = "2", "moderately serious" = "3", "very serious" = "4", "extremely serious" = "5")
q25_data$Q25_outside_uk <- fct_recode(q25_data$Q25_outside_uk, "not at all serious" = "1", "somewhat serious" = "2", "moderately serious" = "3", "very serious" = "4", "extremely serious" = "5")
# Change names of rows to question text
names(q25_data) <- c("You and your family in the UK", "People in your local area or city", "The UK as a whole", "Your family and/or friends living outside the UK")
q25_likert_table <- likert(as.data.frame(q25_data))

所以我想的是,我可以使用 q25_names <- names(select(q25_data, Q25_self_and_family:Q25_outside_uk)) 获取列名,然后使用 lapply() 和修改后的函数,如下所示: test <-lapply(q25_names, function(q25_column) {q25_data$q25_column <- factor(select(q25_data, q25_column), ordered = TRUE, levels = c("1", "2", "3", "4", "5"))}) 然而,我'我用这种方法很快就无处可去。怀疑我在这里遗漏了一些明显的东西,但我已经在 SE 上查看了十几个示例,但仍然没有找到成功的方法。

您可以使用管道“简化”您的工作流程,例如如下图所示。简而言之,您可以使用 across 将函数应用于每一列,并使用 factor,这可以方便地让您将标签设置为因子水平。然后通过管道将结果转换为 data.frame,然后转换为 likert 对象。

require(likert)
require(dplyr)

q25_data <- tibble(
    Q25_self_and_family = c(4, 2, 3, 3, 5, 3), 
    Q25_local_area = c(3, 3, 3, 3, 5, 3), 
    Q25_uk = c(4, 3, 3, 3, 5, 2), 
    Q25_outside_uk = c(4, 4, 3, 3, 5, 4))

# Set up levels text for question responses
q25_levels <- paste(c("not at all", "somewhat", "moderately", "very", "extremely"),  
    "serious")
q25_likert_table <- q25_data %>% 
    mutate(across(everything(), 
        factor, ordered = TRUE, levels = 1:5, labels=q25_levels)) %>% 
    as.data.frame %>% 
    likert

reprex package (v2.0.1)

于 2022-03-07 创建