I want to recode a variable to missing if it takes on one of three values in dplyr. Consider the following data frame have:
id married hrs_workperwk
1 1 40
2 1 55
3 1 70
4 0 -1
5 1 99
6 0 -2
7 0 10
8 0 40
9 1 45
-1, -2, and 99 are illegal values. The new data frame want should look like this:
id married hrs_workperwk
1 1 40
2 1 55
3 1 70
4 0 NA
5 1 NA
6 0 NA
7 0 10
8 0 40
9 1 45
I could use base R to solve this quickly, but dplyr is often convenient to work in when I'm already using mutate(). Alas, that means I currently use multiple nested if_else() functions:
want <- mutate(have,
hrs_workperwk = if_else(hrs_workperwk < 0, as.numeric(NA),
if_else(hrs_workperwk = 99, as.numeric(NA), hrs_workperwk)))
Is there a way to do this with only one if_else() function? Ideally something like this:
want <- mutate(have,
hrs_workperwk = if_else(hrs_workperwk = c(-2, -1, 99), as.numeric(NA), hrs_workperwk))