Search code examples
rqualtrics

How to combine multiple ethnicity columns into one in R?


I'm using a multi-answer ethnicity question from a Qualtrics survey in my dataset and am looking to collapse multiple columns into one.

My data looks like this:

White/Caucasian Black/African American Hispanic Pacific Islander/Native Hawaiian American Indian/Alaskan Native
1 - 1 - -
1 - - - -
- - - - 1
- - - 1 -

I'm trying to get it to look like this:

Race
Multiple
White
American Indian/Alaskan Native
Pacific Islander/Native Hawaiian

Is there a way to do this in R? I have been working on this for hours!


Solution

  • Another tidyverse option:

    library(tidyverse)
    
    df %>%
      mutate(id = row_number(),
             across(everything(), ~ na_if(.x, "-"))) %>%
      pivot_longer(-id, names_to = "Race", values_drop_na = TRUE) %>%
      group_by(id) %>%
      mutate(Race = ifelse(n() > 1, "Multiple", Race)) %>%
      distinct() %>% 
      ungroup() %>%
      select(Race)
    

    Output

      Race                            
      <chr>                           
    1 Multiple                        
    2 White/Caucasian                 
    3 American Indian/Alaskan Native  
    4 Pacific Islander/Native Hawaiian
    

    Data

    df <- structure(list(`White/Caucasian` = c("1", "1", "-", "-"), `Black/African American` = c("-", 
    "-", "-", "-"), Hispanic = c("1", "-", "-", "-"), `Pacific Islander/Native Hawaiian` = c("-", 
    "-", "-", "1"), `American Indian/Alaskan Native` = c("-", "-", 
    "1", "-")), row.names = c(NA, -4L), class = "data.frame")