How to remove the apostrophe in 'Don't Know' observations in R

254 Views Asked by At

I feel like I have tried everything, but I still cannot figure out how to remove the apostrophe in 'Don't Know' observations. My desired outcome is for the observations to say 'Dont Know.'

I have tried so many of the solutions mentioned here, but I still cannot fix it. I have multiple variables that I am struggling to change. The codes always run, but they don't actually change anything.

I have tried:

1. thesis <-  as.data.frame(sapply(thesis, function(doaway) gsub("'", "", doaway)))
2. for(i in 1:ncol(thesis)){
  thesis[,i] <- gsub("'","",thesis[,i])
}
3. thesis$doaway[thesis $doaway== "Don\'t Know"] = "Dont Know"
4. thesis$doaway[thesis $doaway== "Don"\'"t Know"] = "Dont Know"
5. thesis$doaway[thesis $doaway== "Don'\''t Know"] = "Dont Know"

Still, I am unable to do this at both the individual observations level and the whole data level.

1

There are 1 best solutions below

0
Gregor Thomas On

It's possible that your apostrophe is a "fancy quote" or something like that. You could try a more general regex solution like remove all punctuation from a string:

gsub(pattern = '[[:punct:]]', replacement = '', doaway)

You could also probably copy/paste the fancy quote (or the whole "Don't Know" string with its fancy quote) into one of your other methods.