In a data frame I have text like
"X1" "X2"
"1" 53 "'[email protected]'"
"2" 54 "'[email protected]'"
"3" 55 "'[email protected]'"
"4" 56 "'[email protected]'"
How do I remove the single quotes from the string in 2nd column?
Use the String. replaceAll() method to remove all double quotes from a string, e.g. str. replaceAll('"', '') . The replace() method will return a new string with all double quotes removed.
noquote() function in R Language is used to prints strings without quotes.
No escaping is used with single quotes. Use a double backslash as the escape character for backslash.
Just use "\"" or '"' to match a single double quote.
To replace text, use (g
)sub
:
result <- gsub("'", '', yourString)
The function is vectorised so you can apply it directly to your data frame without the need for a loop or an apply
:
df$X2 <- gsub("'", '', df$X2)
I know the question states otherwise, but what he actually wants to do is to unwrap this 2nd column, that is to remove tailing and leading single quotes. This can be done with a slightly enhanced regex:
gsub("(^')|('$)", "", df$X2)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With