Home > front end >  Replace String in one column from another string in different column based on a flag
Replace String in one column from another string in different column based on a flag

Time:03-25

Let's assume I have a dataset like this one:

df1 = data.frame(Name=c("<HELLO>_World","World","HELLO_<WORLD>"),
                 Generic=c("<HELLO>","<HELLO>","<WORLD>"),
                 Substitution = c("hello1","world","world1"),
                 Flag = c("Yes","No","Yes"))

Now, based on the flag, I'd like to obtain the replacement in the "Name" column of the string in the substitution one, In the end the dataframe should look like this:

final <- data.frame(Name=c("hello1_World","world","HELLO_world1"))

I've tried with something like this:

index <- df1$Flag == "Yes"
df1$Name[index] <- gsub(df1$Generic[index],df1$Substitution[index])

Maybe it should be done in a new column (also acceptable)

CodePudding user response:

library(tidyverse)
df1 %>%
  mutate(new_name = ifelse( 
    Flag == "Yes",
    unlist(purrr::pmap(list(x = Generic, y = Substitution, z = Name), 
                       ~ gsub(..1, ..2, ..3))),
    Name))

#             Name Generic Substitution Flag     new_name
# 1: <HELLO>_World <HELLO>       hello1  Yes hello1_World
# 2:         World <HELLO>        world   No        World
# 3: HELLO_<WORLD> <WORLD>       world1  Yes HELLO_world1

CodePudding user response:

In base R:

df1$result <- Map(function(flag, str, replace, sub) {
  if(flag == 'No') return(str)
  else gsub(sub, replace, str, fixed = TRUE)
}, flag = df1$Flag, str = df1$Name, replace = df1$Substitution, sub = df1$Generic)

df1
#>            Name Generic Substitution Flag       result
#> 1 <HELLO>_World <HELLO>       hello1  Yes hello1_World
#> 2         World <HELLO>        world   No        World
#> 3 HELLO_<WORLD> <WORLD>       world1  Yes HELLO_world1
  •  Tags:  
  • r
  • Related