You can try indexing numeric .
mtcars %>% select(am) %>% mutate(am1= c('zero', 'one')[am+1L])
Or using replace , but this is not useful when replacing multiple elements. It would be best to use factor and specify levels/labels .
mtcars %>% select(am) %>% mutate(am1= replace(replace(am, !am, 'zero'), am==1, 'one') )
Or instead of double replace create a column of zero and replace zero' by one` based on the values ββof "am"
mtcars %>% select(am) %>% mutate(am1= 'zero', am1=replace(am1, am!=0, 'one'))
Another option where you can change multiple elements with the corresponding replacement element is mgsub from qdap
library(qdap) mtcars %>% select(am) %>% mutate(am1= mgsub(0:1, c('zero', 'one'), am))
Update
If you need to use replace to change values ββin one variable based on another,
mtcars %>% select(am,gear) %>% mutate(am= replace(am, gear %in% 4:5, 1000))
source share