Search code examples
rdplyrtextreplace

Writing a regex to replace a specific name with another name in a text column


I have a data set that includes some texts as follows:

df <- data.frame(
  name=c('Chris is nice','ris is nice and ris is old','Risoto is bad','RIS is bad',
         'rising is nice ', 'Prosis is ris and big','Ris is tall','doris is short'))

df %>% select(name)

                        name
1              Chris is nice
2 ris is nice and ric is old
3              Risoto is bad
4                 RIS is bad
5            rising is nice 
6      Prosis is ris and big
7                Ris is tall
8             doris is short

I need to replace only ris names that have no strings before or after and are shown as ris. Such words need to be replaced with LTD.

The outcome of interest would be:

                        name
1              Chris is nice
2 LTD is nice and LTD is old
3              Risoto is bad
4                 LTD is bad
5            rising is nice 
6      Prosis is LTD and big
7                LTD is tall
8             doris is short

Thank you!


Solution

  • I would suggest mutate(name = gsub("\\<ris\\>", "LTD", name, ignore.case = TRUE))

    library(dplyr)
      
    df <- data.frame(
      name=c('Chris is nice','ris is nice and ris is old','Risoto is bad','RIS is bad',
             'rising is nice ', 'Prosis is ris and big','Ris is tall','doris is short'))
    
    df %>% 
      mutate(name = gsub("\\<ris\\>", "LTD", name, ignore.case = TRUE)) %>% 
      select(name)
    
                            name
    1              Chris is nice
    2 LTD is nice and LTD is old
    3              Risoto is bad
    4                 LTD is bad
    5            rising is nice 
    6      Prosis is LTD and big
    7                LTD is tall
    8             doris is short