简体   繁体   English

在 R 中将多个竞赛列合并为一列

[英]Combine multiple race columns into one column in R

I have 11 columns each representing a different race.我有 11 列,每列代表不同的种族。 Participants could select "Yes" or "No" for each column.参与者可以 select 每列“是”或“否”。 I can't figure out how to merge all of these columns into one race variable where the "Yes" and "No" responses are converted to the specific race and if a participant selects more than one race, then have that be coded as "More than one race."我不知道如何将所有这些列合并到一个比赛变量中,其中“是”和“否”响应被转换为特定比赛,如果参与者选择了不止一场比赛,那么将其编码为“不止一场比赛。”

Any input would be appreciated!任何输入将不胜感激!

Example Data示例数据

library(tidyverse)

df <- data.frame(q105_1 = c('', '', 'White', ''), 
                 q105_2 = c('Black', '', 'Black', ''))


twoOrMoreRaces <- df %>% 
  mutate(twoOrMoreMinority = case_when(
    q105_1 == 'White' & q105_2 == 'Black'~ 'Two or More races',
    TRUE ~ 'Single Race Reported'
  ))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM