How to remove single quote from string in R?

In the data frame, I have text like

"X1" "X2" "1" 53 "' all.downtown@enron.com '" "2" 54 "' all.enron-worldwide@enron.com '" "3" 55 "' all.worldwide@enron.com '" "4" 56 "' all_enron_north.america@enron.com '" 

How to remove single quotes from a row in a second column?

+7
r quotes
source share
3 answers

To replace the text, use ( g ) sub :

 result <- gsub("'", '', yourString) 

The function is vectorized, so you can apply it directly to the data frame without the need for a loop or apply :

 df$X2 <- gsub("'", '', df$X2) 
+12
source share
  df[,2] <- gsub("'", '', df[,2], fixed=TRUE) 

I think it is fixed by default, but it is never afraid to be explicit.

Sorry, read the message heading as "How do I remove a (single) single quote from a string in R?"

+2
source share

I know that this question says differently, but what he really wants to do is expand this second column, that is, remove the tail and leading single quotes. This can be done with a slightly extended regular expression:

 gsub("(^')|('$)", "", df$X2) 
0
source share

All Articles