Search code examples
rdata-cleaning

Replacing + by 5 in R


I have a dataset called Price which is supposed to be numeric but is generated as a string because all 5 is replaced by +.

It looks like this:

"99000"    "98300"    "98300"    "98290"    "98310"    "  9831+ " "98310"    "  9830+ " "  9830+ " "  9830+ " "  9829+ " "  9828+ " "  9827+ " "98270" 

I used the gsub function in R to try and replace + by 5. The code I wrote is:

finalPrice<-gsub("+",5,Price)

However, the output is just a bunch of numbers which doesn't make sense for what I intended:

"59595050505,5 59585350505,5 59585350505,5 59585259505,5 59585351505,5 5 5 595853515+5 5,5 59585351505,5 5 5 595853505+5 5,5 5 5 595853505+5

How can I fix this?


Solution

  • The + sign should be escaped. Try this:

    finalPrice<-gsub("\\+",5, Price)