1

I am trying to recode demographic variables. If the variable Q36 equals Other and the variable Q35 equals Yes, I want Other (from Q36) to be changed to Hispanic. If the variable Q36 equals Other and the variable Q35 equals No, I want Other (from Q36) to be changed to Unspecified.

Here's my code so far.

data %>% 
  mutate(Q36 = case_when(Q35 == "Yes" & Q36 == "Other" ~ Q36 == "Hispanic", 
Q35 == "No" & Q36 == "Other" ~ Q36 == "Unspecified")) -> data
  • On the right hand side of `~`, just provide the value (like `~"Hispanic"`), rather than `~Q36=="Hispanic"` – langtang May 19 '22 at 13:22

1 Answers1

2

You are close:

data %>% 
  mutate(Q36 = case_when(Q35 == "Yes" & Q36 == "Other" ~ "Hispanic", 
                 Q35 == "No" & Q36 == "Other" ~ "Unspecified",
                TRUE~Q36))
Onyambu
  • 67,392
  • 3
  • 24
  • 53
  • That did it. Thanks for the help. The only other thing I had to do to complete it was to add {-> data} as seen below: ``` data %>% mutate(Q36 = case_when(Q35 == "Yes" & Q36 == "Other" ~ "Hispanic", Q35 == "No" & Q36 == "Other" ~ "Unspecified", TRUE~Q36)) -> data ``` – Patrick Kielly May 19 '22 at 14:45