r中变量的合并调查列

发布于 2025-01-18 21:23:46 字数 1806 浏览 0 评论 0原文

我正在分析一项非常大的调查,我想通过 4 个问题的几种组合来合并调查的四个部分。下面我创建了一个小例子。一点背景知识:受访者要么回答 q2、q5、q8 或 q9,因为他们只根据 q1 中的答案填写了调查的 4 个部分中的 1 个(此处未显示)。因此,四列中只有一列包含答案(1 或 2),而其他包含 NA。 q2、q5、q8、q9 是类似的问题,具有相同的答案选项,这就是为什么我想将它们组合起来,以使我的数据集更窄,并更容易进一步分析数据。

q2_1 <- c(NA, NA, NA, NA, NA, NA, rep(c(1:2), 1))
q5_1 <- c(NA, NA, NA, NA, rep(c(1:2), 1), NA, NA)
q8_1 <- c(NA, NA, rep(c(1:2), 1), NA, NA, NA, NA)
q9_1 <- c(rep(c(1:2), 1), NA, NA, NA, NA, NA, NA)
q2_2 <- c(NA, NA, NA, NA, NA, NA, rep(c(1:2), 1))
q5_2 <- c(NA, NA, NA, NA, rep(c(1:2), 1), NA, NA)
q8_2 <- c(NA, NA, rep(c(1:2), 1), NA, NA, NA, NA)
q9_2 <- c(rep(c(1:2), 1), NA, NA, NA, NA, NA, NA)

df <- data.frame(q2_1, q5_1, q8_1, q9_1, q2_2, q5_2, q8_2, q9_2)
df

# running df shows: 
  q2_1 q5_1 q8_1 q9_1 q2_2 q5_2 q8_2 q9_2
1   NA   NA   NA    1   NA   NA   NA    1
2   NA   NA   NA    2   NA   NA   NA    2
3   NA   NA    1   NA   NA   NA    1   NA
4   NA   NA    2   NA   NA   NA    2   NA
5   NA    1   NA   NA   NA    1   NA   NA
6   NA    2   NA   NA   NA    2   NA   NA
7    1   NA   NA   NA    1   NA   NA   NA
8    2   NA   NA   NA    2   NA   NA   NA

我想要的最终结果是一个数据框,其中仅包含以 q2_ 开头的问题的列(因此,在示例中为 q2_1 和 q2_2;实际上这个问题大约有 20 个),但将 NA 替换为来自对应q5_、q8_和q_9。

# desired end result
  q2_1 q2_2 
1    1   1
2    1   2
3    1   1   
4    2   2   
5    1   1   
6    2   2   
7    1   1   
8    2   2   

对于单个问题,我使用下面的代码完成了此操作,但这是非常手动的,因为 q2、q5、q8 和 q9 都达到 _20,我正在寻找一种更加自动化的方法。

# example single question
library(tidyverse)
df <- df %>%
  mutate(q2_1 = case_when(!is.na(q2_1) ~ q2_1, 
                          !is.na(q5_1) ~ q5_1,
                          !is.na(q8_1) ~ q8_1,
                          !is.na(q9_1) ~ q9_1))

我希望我能很好地解释自己并期待一些方向!

I am analyzing a very large survey in which I want to combine four parts of the survey, through several combinations of 4 questions. Below I have created a small example. A little background: a respondent either answered q2, q5, q8 or q9, because they only filled in 1 of 4 parts of the survey based on their answer in q1 (not shown here).Therefore, only one of the four columns contains an answer (1 or 2), while the others contain NAs. q2, q5, q8, q9 are similar questions that have the same answer options, which is why I want to combine them to make my dataset less wide and make it easier to further analyze the data.

q2_1 <- c(NA, NA, NA, NA, NA, NA, rep(c(1:2), 1))
q5_1 <- c(NA, NA, NA, NA, rep(c(1:2), 1), NA, NA)
q8_1 <- c(NA, NA, rep(c(1:2), 1), NA, NA, NA, NA)
q9_1 <- c(rep(c(1:2), 1), NA, NA, NA, NA, NA, NA)
q2_2 <- c(NA, NA, NA, NA, NA, NA, rep(c(1:2), 1))
q5_2 <- c(NA, NA, NA, NA, rep(c(1:2), 1), NA, NA)
q8_2 <- c(NA, NA, rep(c(1:2), 1), NA, NA, NA, NA)
q9_2 <- c(rep(c(1:2), 1), NA, NA, NA, NA, NA, NA)

df <- data.frame(q2_1, q5_1, q8_1, q9_1, q2_2, q5_2, q8_2, q9_2)
df

# running df shows: 
  q2_1 q5_1 q8_1 q9_1 q2_2 q5_2 q8_2 q9_2
1   NA   NA   NA    1   NA   NA   NA    1
2   NA   NA   NA    2   NA   NA   NA    2
3   NA   NA    1   NA   NA   NA    1   NA
4   NA   NA    2   NA   NA   NA    2   NA
5   NA    1   NA   NA   NA    1   NA   NA
6   NA    2   NA   NA   NA    2   NA   NA
7    1   NA   NA   NA    1   NA   NA   NA
8    2   NA   NA   NA    2   NA   NA   NA

My desired end result would be a dataframe with only columns for questions starting with q2_ (so, in the example that would be q2_1 and q2_2; in reality there's about 20 for this question), but with the NAs replaced for the answer options from the corresponding q5_, q8_, and q_9.

# desired end result
  q2_1 q2_2 
1    1   1
2    1   2
3    1   1   
4    2   2   
5    1   1   
6    2   2   
7    1   1   
8    2   2   

For single questions, i've done this using the code below, but this is very manual and because q2, q5, q8, and q9 both go up to _20, I'm looking for a way to automate this more.

# example single question
library(tidyverse)
df <- df %>%
  mutate(q2_1 = case_when(!is.na(q2_1) ~ q2_1, 
                          !is.na(q5_1) ~ q5_1,
                          !is.na(q8_1) ~ q8_1,
                          !is.na(q9_1) ~ q9_1))

I hope I explained myself well enough and looking forward for some directions!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦境 2025-01-25 21:23:46

这是一种方法,使用coalesce

df %>%
  mutate(q2_1 = do.call(coalesce, across(ends_with('_1'))),
         q2_2 = do.call(coalesce, across(ends_with('_2')))) %>%
  select(q2_1, q2_2)

#>   q2_1 q2_2
#> 1    1    1
#> 2    2    2
#> 3    1    1
#> 4    2    2
#> 5    1    1
#> 6    2    2
#> 7    1    1
#> 8    2    2

Here's one way, using coalesce:

df %>%
  mutate(q2_1 = do.call(coalesce, across(ends_with('_1'))),
         q2_2 = do.call(coalesce, across(ends_with('_2')))) %>%
  select(q2_1, q2_2)

#>   q2_1 q2_2
#> 1    1    1
#> 2    2    2
#> 3    1    1
#> 4    2    2
#> 5    1    1
#> 6    2    2
#> 7    1    1
#> 8    2    2
缺⑴份安定 2025-01-25 21:23:46
q2_1 <- c(NA, NA, NA, NA, NA, NA, rep(c(1:2), 1))
q5_1 <- c(NA, NA, NA, NA, rep(c(1:2), 1), NA, NA)
q8_1 <- c(NA, NA, rep(c(1:2), 1), NA, NA, NA, NA)
q9_1 <- c(rep(c(1:2), 1), NA, NA, NA, NA, NA, NA)
q2_2 <- c(NA, NA, NA, NA, NA, NA, rep(c(1:2), 1))
q5_2 <- c(NA, NA, NA, NA, rep(c(1:2), 1), NA, NA)
q8_2 <- c(NA, NA, rep(c(1:2), 1), NA, NA, NA, NA)
q9_2 <- c(rep(c(1:2), 1), NA, NA, NA, NA, NA, NA)

df <- data.frame(q2_1, q5_1, q8_1, q9_1, q2_2, q5_2, q8_2, q9_2)
df
#>   q2_1 q5_1 q8_1 q9_1 q2_2 q5_2 q8_2 q9_2
#> 1   NA   NA   NA    1   NA   NA   NA    1
#> 2   NA   NA   NA    2   NA   NA   NA    2
#> 3   NA   NA    1   NA   NA   NA    1   NA
#> 4   NA   NA    2   NA   NA   NA    2   NA
#> 5   NA    1   NA   NA   NA    1   NA   NA
#> 6   NA    2   NA   NA   NA    2   NA   NA
#> 7    1   NA   NA   NA    1   NA   NA   NA
#> 8    2   NA   NA   NA    2   NA   NA   NA

library(tidyverse)

suffix <- str_c("_", 1:2)

map_dfc(.x = suffix,
    .f = ~ transmute(df, !!str_c("q2", .x) := rowSums(across(ends_with(.x
    )), na.rm = T)))
#>   q2_1 q2_2
#> 1    1    1
#> 2    2    2
#> 3    1    1
#> 4    2    2
#> 5    1    1
#> 6    2    2
#> 7    1    1
#> 8    2    2

q2_1 <- c(NA, NA, NA, NA, NA, NA, rep(c(1:2), 1))
q5_1 <- c(NA, NA, NA, NA, rep(c(1:2), 1), NA, NA)
q8_1 <- c(NA, NA, rep(c(1:2), 1), NA, NA, NA, NA)
q9_1 <- c(rep(c(1:2), 1), NA, NA, NA, NA, NA, NA)
q2_2 <- c(NA, NA, NA, NA, NA, NA, rep(c(1:2), 1))
q5_2 <- c(NA, NA, NA, NA, rep(c(1:2), 1), NA, NA)
q8_2 <- c(NA, NA, rep(c(1:2), 1), NA, NA, NA, NA)
q9_2 <- c(rep(c(1:2), 1), NA, NA, NA, NA, NA, NA)

df <- data.frame(q2_1, q5_1, q8_1, q9_1, q2_2, q5_2, q8_2, q9_2)
df
#>   q2_1 q5_1 q8_1 q9_1 q2_2 q5_2 q8_2 q9_2
#> 1   NA   NA   NA    1   NA   NA   NA    1
#> 2   NA   NA   NA    2   NA   NA   NA    2
#> 3   NA   NA    1   NA   NA   NA    1   NA
#> 4   NA   NA    2   NA   NA   NA    2   NA
#> 5   NA    1   NA   NA   NA    1   NA   NA
#> 6   NA    2   NA   NA   NA    2   NA   NA
#> 7    1   NA   NA   NA    1   NA   NA   NA
#> 8    2   NA   NA   NA    2   NA   NA   NA

library(tidyverse)

suffix <- str_c("_", 1:2)

map_dfc(.x = suffix,
    .f = ~ transmute(df, !!str_c("q2", .x) := rowSums(across(ends_with(.x
    )), na.rm = T)))
#>   q2_1 q2_2
#> 1    1    1
#> 2    2    2
#> 3    1    1
#> 4    2    2
#> 5    1    1
#> 6    2    2
#> 7    1    1
#> 8    2    2

Created on 2022-04-04 by the reprex package (v2.0.1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文