Remove characters prior to parentheses but after the preceding comma in R

Viewed 133

I have the following dataframe:

df<-c("red apples,(golden,red delicious),bananas,(cavendish,lady finger),golden pears","yellow pineapples,red tomatoes,(roma,vine),orange carrots")

I want to remove the word preceding a comma and parentheses so my output would yield:

[1] "golden,red delicious),cavendish,lady finger),golden pears" "yellow pineapples,roma,vine),orange carrots"

Ideally, the right parenthesis would be removed as well. But I can manage that delete with gsub.

I feel like a lookbehind might work but can't seem to code it correctly.

Thanks!

edit: I amended the dataframe so that the word I want deleted is a string of two words.

2 Answers

We can use base R with gsub to remove the characters. We match a word (\\w+) followed by space (\\s+) followed by word (\\w+) comma (,) and (, replace with blank ("")

gsub("\\w+\\s+\\w+,\\(", "", df)
#[1] "golden,red delicious),cavendish,lady finger),golden pears" 
#[2] "yellow pineapples,roma,vine),orange carrots"  

Or if the , is one of the patterns to check for the words, we can create the pattern with characters that are not a ,

gsub("[^,]+,\\(", "", df)
#[1] "golden,red delicious),cavendish,lady finger),golden pears" 
#[2] "yellow pineapples,roma,vine),orange carrots" 

Using the tidyverse package stringr, I was able to make your data appear the way you'd want it with two function calls separated by a pipe. The pipe comes from the package magrittr which loads with dplyr and/or tidyverse.

I used stringr::str_replace_all to perform two substitutions which remove the words you wanted to take out. Note the syntax for multiple substitutions within this function.

    str_replace_all( c( "first string to get rid of" = "string to replace it with", "second string to get rid of" = "second replacement string")

You might find it more intuitive to combine all the "get rid of strings" first followed by combining the replacement strings, but each element within the c() is the string to be replaced (in quotes) connected to its replacement (also in quotes) with "=". Each of those replaced=replacement pairs is separated by a comma.

Using str_replace, I first took out all text which starts with "," and ends with ",)" using this regular expression ",[a-z ]+,\\(" which refers to comma, followed by any number of lowercase letters and spaces (allowing for chunks with multiple words to be detected) followed by ",(". Note the escape for the "(". If you thought there might be capital letters you would use [a-zA-Z ] instead. In either case, note the space before the "]".

Because you wanted to take out the word, but not the comma preceding it, I replaced the removed text with ",".

This doesn't remove "red apples" in the first string because it doesn't follow a comma. The expression "^[a-z ]+,\\(" refers to any number of lowercase letters and spaces coming before ",(" at the beginning of the string (the ^ "anchors" your pattern to the beginning of the string). Therefore it removes "red apples" or any other example where the text you want to remove starts the string. For these cases, it makes sense to replace it with nothing ("") because you want the first character of the remaining string to appear at the beginning.

Together, the two substitutions remove the offending text whether it starts the string or is in the middle of it or ends it so in that sense it's more or less generalized.

str_remove_all("\\)") removes the right parentheses throughout

    library(stringr)
    library(magrittr)
    df<-c("red apples,(golden,red delicious),bananas,(cavendish,lady finger),
    golden pears","yellow pineapples,red tomatoes,(roma,vine),orange carrots")


    str_replace_all(df, c(",[a-z ]+,\\(" = ",", 
                  "^[a-z ]+,\\(" = "")) %>%
    str_remove_all("\\)")

    [1] "golden,red delicious,cavendish,lady finger,golden pears"
    [2] "yellow pineapples,roma,vine,orange carrots"  
Related