Simple recoding of string variables

Hi,
I have this simple df.

source <- 
data.frame(
  stringsAsFactors = FALSE,
                         URN = c("aaa",
                                 "bbb","ccc","ddd","eee","fff","ggg","hhh"),
                       Model = c("corsa",
                                 "corsa electric"," opel corsa","corsa","corsa-e",
                                 "corsa e","corsa","corsa"),
                          EV = c("Other",
                                 "Electric","Electric","Other","Electric",
                                 "Electric","Other","Electric")
          )
source

I have noticed that information included must be adjusted. "EV" describes electric models so:
IF EV="Electric" than Model should be changed from "Corsa" (or from "Opel corsa") to "Corsa E".

What is the easiest way of doing that? Can you help?

One clunky way:

library(dplyr)
library(stringr)
source <- source %>% mutate(Model = if_else(EV == "Electric" & str_detect(string = source$Model, pattern = "corsa") == TRUE, "corsa-e", Model))

@ach Actually, stringr::str_detect() returns a logical vector, so there is really no need to test equality with TRUE.

1 Like

Hey @Slavek

The solution was actually provided by @ach I was just pointing out that the code could have been just a little shorter. Thanks for giving him the solution instead.

1 Like

Thanks for pointing that out @gueyenono :slight_smile:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.