Home > Software engineering >  Replace characters by matching string using several options
Replace characters by matching string using several options

Time:07-05

I am trying to replace the parts of string by several matching expressions. I have found how to apply the str_replace_all on the specific column. But I do not know how to apply it within the dataframe with the dplyr piping?

Dummy example:

d <- data.frame(nam = c('aaa_string', 'bb_salala', 'cc_bububub'))

# applied on the d$nam vector every thing is working
d$nam %>% 
     str_replace_all(c("aaa" = "1", "bb" = "2", "cc" = "33"))

Using the same statement within the mutate it suddenly does not work, claiming that argument "pattern" is missing in str_replace_all. How to fix it?

Expected output:

nam
"1_string"   
"2_salala"   
"33_bububub"

CodePudding user response:

Not clear about how the mutate statement was used. The following works well. In addition, if the mutate was masked by plyr::mutate, specify dplyr::mutate

library(dplyr)
library(stringr)
d <- d %>%
   dplyr::mutate(nam = str_replace_all(nam, 
  c("aaa" = "1", "bb" = "2", "cc" = "33")))

-output

     nam
1   1_string
2   2_salala
3 33_bububub

CodePudding user response:

Here is an alternative approach:

library(dplyr)
library(tidyr)

d %>% 
  separate(nam, c("a", "b")) %>% 
  mutate(a = case_when(a == "aaa" ~ "1",
                       a == "bb" ~ "2",
                       a == "cc" ~ "33", 
                       TRUE ~ NA_character_)) %>% 
  mutate(nam = paste(a,b, sep = "_"), .keep="unused")
        nam
1   1_string
2   2_salala
3 33_bububub
  • Related