Removing Asterisk and Brackets from a column

Hi All,

I need help with removal of asterisk and brackets from my dataset.

My dataset which includes * at the end of few values in a particular column such as ABC Imports* . It also includes some values within brackets like Imports (ABC).

I am trying to remove only * and only brackets in these values. I tried str_replace, str_remove and even gsub, however, it doesn't work .

Example of str_replace
df <- df %>%
str_replace(Column_name, "Imports*", "Imports")

It throws me error saying "unused argument ("Imports")

Can you please help? Thank you!

Kamlesh Singh

Both asterisks and parentheses are special characters in regular expressions and need to be escaped with backslashes. Here is a simple example of what I think you want to do. The pipe, |, in the regular expression means "or".


DF <- data.frame(COL1 = c("XYZ ABC", "Imports*", "XYZ (ABC) Imports"))
#>                COL1
#> 1           XYZ ABC
#> 2          Imports*
#> 3 XYZ (ABC) Imports
DF <- DF %>% mutate(COL1 = str_replace_all(COL1, "\\*|\\(|\\)", ""))
#>              COL1
#> 1         XYZ ABC
#> 2         Imports
#> 3 XYZ ABC Imports

Created on 2020-06-08 by the reprex package (v0.3.0)

Hi @ksingh19,

Take a look at this:

d <- tibble(my_text = c("ABC Imports*", "Imports (ABC)"))
d %>% 
  mutate(my_text_clean = str_replace_all(string = my_text,
                                         pattern = "[\\*\\(\\)]",
                                         replacement = ""))

Hope it helps :slightly_smiling_face:

Thank you so much!
So backslash is the trick!

Thanks Leon! Yes, it works! Backslash is new to me in str_replace!
Thanks a bunch!

Kindly remember to mark the solution :+1:

...and the backslashes are there for a reason - I recommend, that you read this chapter in R for Data Science

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