This question already has an answer here:
- Set certain values to NA with dplyr 3 answers
I regularly need to change the values of a variable based on the values on a different variable, like this:
mtcars$mpg[mtcars$cyl == 4] <- NA
I tried doing this with dplyr
but failed miserably:
mtcars %>%
mutate(mpg = mpg == NA[cyl == 4]) %>%
as.data.frame()
How could I do this with dplyr
?