'Is there a way I can pool together 3 variables based on conditions into a new column without NAs to avoid the "argument mu needs to be a numerical"

So I have to make a new column C5 based on responses in C5a,C5b,C5c, which are dependent on responses to question C3. C5a is shown to a respondent if they select 1 in C3, C5b is shown if they select 2 in C3, and C5c is shown if they select 3 in C3. I can't figure out what the last else command should be. If I put in NA then I get the "argument mu needs to be a non zero numeric variable" error, and if I put in 3 (which is not a coded variable) it muddles up the data and even after I drop all values with 3 I get NAs. I tried omitting NAs but then I get an empty dataset although the filtered dataset does not have that many NAs (roughly 2250 out of the 5000s observations)

dput( filteredset$combinedC5 <- ifelse(filteredset$C3==1 , filteredset$C5a, 
                         ifelse(filteredset$C3==2 , filteredset$C5b, 
                                ifelse(filteredset$C3==3 , filteredset$C5c, 3 ) )))
r


Solution 1:[1]

library(tidyverse)

data <- tibble(C3 = c(1, 1, 2, 3, NA))

data <-
  data %>%
  mutate(
    C5a = C3 == 1,
    C5b = C3 == 2,
    C5c = C3 == 3,
    C5 = case_when(
      C3 == 1 ~ C5a,
      C3 == 2 ~ C5b,
      C3 == 3 ~ C5c
    )
  )
data
#> # A tibble: 5 × 5
#>      C3 C5a   C5b   C5c   C5   
#>   <dbl> <lgl> <lgl> <lgl> <lgl>
#> 1     1 TRUE  FALSE FALSE TRUE 
#> 2     1 TRUE  FALSE FALSE TRUE 
#> 3     2 FALSE TRUE  FALSE TRUE 
#> 4     3 FALSE FALSE TRUE  TRUE 
#> 5    NA NA    NA    NA    NA

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 danlooo