Search code examples
rdplyrsubstr

conditional mutate by dplyr


I want to create a new column in my dataset that: i) drop the last 1 character if the word itself starts with "vi"; and ii) drop the last 2 characters if the word itself does not start with "vi". I know how to work on that in R, like below:

iris$Species <- as.character(iris$Species)
iris$Species_mod <- substr(iris$Species,
                           1,
                           ifelse(grepl('^vi',iris$Species),
                                  nchar(iris$Species)-1,
                                  nchar(iris$Species)-2))

But I have a hard time in deciphering the mutate, if_else and matches in dplyr. Can anyone enlighten me? Thanks!


Solution

  • Same idea, except you explicitly need to convert your factor to a string

    iris = mutate(iris, Species_mod = substr(Species, 1, nchar(as.character(Species)) - 
        ifelse(grepl('^vi', Species), 1, 2)))