118

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?

luciano
  • 11,848
  • 27
  • 77
  • 118

1 Answers1

296

We can use replace to change the values in 'mpg' to NA that corresponds to cyl==4.

mtcars %>%
     mutate(mpg=replace(mpg, cyl==4, NA)) %>%
     as.data.frame()
akrun
  • 674,427
  • 24
  • 381
  • 486
  • If in the rows given by `cyl==4` I want the values of `mpg` to be those of `qsec`, I would do `mtcars %>% mutate(mpg=replace(mpg, cyl==4, qsec))`, but then I get an error... – iago Jun 06 '19 at 16:00
  • 3
    @iago WIth `replace`, you need to subset the `values` as well i.e. `replace(mpg, cyl == 4, qsec[cyl==4])` as the replacement values are selecting the whole column – akrun Jun 06 '19 at 16:03
  • I've just tried and I got the same error. Sorry, not in this case, but in my example I am trying – iago Jun 06 '19 at 16:04
  • 1
    @iago Not getting any error for me `mtcars %>% mutate(mpg=replace(mpg, cyl==4, qsec[cyl == 4]))`. It could be a different scenario. are you comparing a single value or multiple value? – akrun Jun 06 '19 at 16:06
  • 1
    Yes!, I solved with a which: `qsec[which(cyl==4)]`. Thanks! I assume my error was due to have missing values in my `cyl` variable. – iago Jun 06 '19 at 16:07
  • 2
    @iago Yes, `==` with `NA` returns `NA` you can use `%in%` instead – akrun Jun 06 '19 at 16:12