Search code examples
rnormalizationgsubdata-cleaning

R gsub not removing '?' in data


I have a large df that I am trying to clean up, it goes something like this

 Name1. Name2. Var1. Var2....
 name?    ...   ...   ...
 name?    ...   ...   ...

So the Names are encrypted and they do include special characters that I do NOT want to remove.

I have been using gsub(), but it does not remove the '?': I just want to remove the single '?' at the end of the names that Excel somehow added on.

MyData$Name1 <- gsub("?", "", MyData$Name1)

Nothing seem to have changed and I do not get any error codes.

    Name1. Name2. Var1. Var2....
    name?    x     a     1 ...
    name?    y     b     2 ...

Does anyone have any prior experience with something like this?


Solution

  • ? is a metacharacter, and has a special meaning in regular expressions. In order to get a literal question mark, you must escape it. Use:

    gsub("\\?", "", MyData$Name1)