Home > database >  How do I change a value within a variable based on the value of another variable in R?
How do I change a value within a variable based on the value of another variable in R?

Time:05-20

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

CodePudding user response:

You are close:

data %>% 
  mutate(Q36 = case_when(Q35 == "Yes" & Q36 == "Other" ~ "Hispanic", 
                 Q35 == "No" & Q36 == "Other" ~ "Unspecified",
                TRUE~Q36))
  • Related