首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >合并改良的Likert类别

合并改良的Likert类别
EN

Stack Overflow用户
提问于 2022-10-18 17:42:36
回答 1查看 18关注 0票数 0

我目前正在使用Likert包,它要求我的Likert矩阵表采用不同的格式。为此,我使用了以下代码:

代码语言:javascript
复制
LikertQ9_1 <- SurveyClean2 |> 
  dplyr::select(Q9_1) |> 
  mutate(Question = "Grazing or Forage Production") |> 
  group_by(Question, Q9_1) |> 
  count() |> 
  ungroup() |> 
  pivot_wider(names_from = Q9_1, values_from = n)

然而,我也有Q9_2通过Q9_10,这也需要重新格式化。有什么办法可以散装地做这件事,还是在最后把它们结合起来?我尝试过合并并离开它们,但它破坏了格式。

原始格式如下所示,仅供参考:

新格式如下所示,仅供参考。它只适用于Q9_1,我需要为2-10添加其他行:

Dataframe样本:

代码语言:javascript
复制
SurveyClean2 <- dplyr::tibble(
      Q9_1 = c("Not Interested", "Not Interested", "NA", "Slightly Interested", "NA"),
      Q9_2 = c("Extremely Interested", "Not Interested", "Somewhat Interested", "NA", "NA"),
      Q9_3 = c("Not Interested", "Extremely Interested", "Slightly Interested", "Somewhat Interested", "Not Interested"),
      Q9_4 = c("Not Interested", "Extremely Interested", "Slightly Interested", "Somewhat Interested", "Not Interested"), 
      Q9_5 = c("Slightly Interested", "Extremely Interested", "Slightly Interested", "Somewhat Interested", "Not Interested"),
      Q9_6 = c("Not Interested", "Extremely Interested", "Slightly Interested", "Somewhat Interested", "NA"),
      Q9_7 = c("Not Interested", "Extremely Interested", "Slightly Interested", "Extremely Interested", "NA"),
      Q9_8 = c("Not Interested", "Extremely Interested", "Somewhat Interested", "Somewhat Interested", "NA"),
      Q9_9 = c("NA", "Extremely Interested", "Slightly Interested", "Somewhat Interested", "NA"),
      Q9_10 = c("Not Interested", "Slightly Interested", "Slightly Interested", "Somewhat Interested", "NA"))
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-10-18 17:59:17

这里有一个使用tidyverse的解决方案。我对您的数据做了一些可能不正确的假设--如果您能够提供一些样本数据来显示您的调查数据是如何构造的,这将是有帮助的:

代码语言:javascript
复制
library(dplyr)
library(tidyr)
library(purrr)

# your survey data
SurveyClean2 <- dplyr::tibble(
  Q9_1 = c("Agree", "Disagree", "Neutral", "Agree"),
  Q9_2 = rep("Agree", 4),
  Q9_3 = c("Disagree", "Disagree", "Neutral", "Agree")
)

# function to clean data
clean_survey <- function(data, column, question) {
  data %>% 
    dplyr::select(all_of({{column}})) %>% 
    dplyr::mutate(Question = question) %>% 
    dplyr::group_by(Question, across(1)) %>% 
    dplyr::count() %>% 
    dplyr::ungroup() %>% 
    tidyr::pivot_wider(names_from = 2, values_from = n)
}

# table that contains survey questions/columns and the question name
survey_table <- dplyr::tibble(
  column = c("Q9_1", "Q9_2", "Q9_3"),
  question = c("Grazing or Forage Production", "Coffee or Tea", "Something else")
)


# loop through your data and clean it, then bind as dataframe
purrr::map2_df(survey_table$column, survey_table$question, function(x, y){
  clean_survey(SurveyClean2, x, y)
})
#> # A tibble: 3 × 4
#>   Question                     Agree Disagree Neutral
#>   <chr>                        <int>    <int>   <int>
#> 1 Grazing or Forage Production     2        1       1
#> 2 Coffee or Tea                    4       NA      NA
#> 3 Something else                   1        2       1

如果您想用0替换NA

代码语言:javascript
复制
purrr::map2_df(survey_table$column, survey_table$question, function(x, y){
  clean_survey(SurveyClean2, x, y)
}) %>% 
  mutate(across(everything(), ~ifelse(is.na(.), 0, .)))
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74115252

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档