Skip to content
Prev 366450 / 398502 Next

Regex problem

Doubtful. You probably only have a single apostrophe and no "ending quote". In fact when I run your `problemdf`, the `make.names` function (called by data.frame) changed the apostrophe into a period. To actually get a trailing apostrophe with `data.frame` you would need to set check.names=FALSE:

df1 <- data.frame("WhatAmI\'" = 1:5, "WhoAreYou" = 11:15, check.names=FALSE)
colnames(df1)
#[1] "WhatAmI'"  "WhoAreYou"
There is no double quote in that name. Now to remove the offending apostrophe (or even multiple instances of them) just do this:

names(df) <- gsub( "\\'", "", names(df)
See above.
I learned regex by reading the ?regex page, and by looking up and working through questions on R-help by Gabor Grothendeick:

http://markmail.org/search/?q=list%3Aorg.r-project.r-help+regex#query:list%3Aorg.r-project.r-help%20regex%20from%3A%22Gabor%20Grothendieck%22+page:1+state:facets


There are also several online sites where you can get an expression by expression readout of what your regexes are doing. They do need the understanding that hte escape character for R and regex are the same and the means they need to be doubled in hte pattern arguments (but _not_ the replacement arguments).