Home > database >  Delete Specific Character Between Quotation Marks in R
Delete Specific Character Between Quotation Marks in R

Time:06-08

In my data frame, I have a column of strings. I would like to get rid of the comma between the string in double quotation marks, i. e., the comma between "Address" and "Unit".

I do NOT want to get rid of all commas.

Situation at hand

'Account1, "1234 Example Address, Unit A", Jupiter , FL, Palm Beach, 33477, United States'

Desired

' Account1, "1234 Example Address Unit A", Jupiter , FL, Palm Beach, 33477, United States '

Edit:

Was advised to mention the comma in the string will not always be between the words "Address" and "Unit"

CodePudding user response:

You could do:

sub('^(.*\". ),(. \".*$)', '\\1\\2', str)
#> [1] "Account1, \"1234 Example Address Unit A\", Jupiter , FL, Palm Beach, 33477, United States "

CodePudding user response:

We may also do

gsub(',(?=[^"]*"(?:[^"]*"[^"]*")*)', '', str1, perl = TRUE)
[1] " Account1 \"1234 Example Address Unit A\", Jupiter , FL, Palm Beach, 33477, United States "
  • Related