You can use the replace function:
cars %>% mutate(speed = replace(speed, speed < 30, 0))
The ifelse condition will also work:
cars %>% mutate(speed = ifelse(speed < 30, 0, speed))
I tested this on a data frame of a million rows and replace did approximately one-eighth of the ifelse time.
library(microbenchmark) set.seed(2) dat = data.frame(x=runif(1e6, 0, 1000), y=runif(1e6, 0, 1000)) microbenchmark( replace=dat %>% mutate(x=replace(x, x<200, 0)), ifelse=dat %>% mutate(x=ifelse(x<200, 0, x)), if_else=dat %>% mutate(x=if_else(x<200, 0, x)), times=100 )
Unit: milliseconds expr min lq mean median uq max neval cld replace 8.352943 9.55682 18.16755 11.45507 15.33215 224.8759 100 a ifelse 71.782371 87.37754 165.95928 95.12722 262.73016 287.3633 100 c if_else 39.947845 47.83934 88.72291 51.99449 59.76760 251.0381 100 b
source share