I am trying to take a character vector of dollar values that is poorly formatted and turn it into a numeric. The values are formatted as in the following vector, with a leading and trailing space, a comma, and a dollar sign:
x <- c(" 18,000.50 ", " $1,240.30 ", " $125.00 ")
I am trying to use the following function to get rid of all characters other than the digits and the dot, but it isn't working:
trim_currency <- function(x) grep("\$([0-9.]*)\,([0-9.]*)", x, values=TRUE)
I got the regex code
\$([0-9.]*)\,([0-9.]*)
to run successfully with this regex tester http://regex101.com/r/qM2uG0
When I run it in R, I get the following error:
Error: '\$' is an unrecognized escape in character string starting "\$"
Any ideas about how I can do this in R?
Thanks to ndoogan for his response. That solves this particular issue. However, if I wanted to make it more general, I would ask:
How could I use R/regex to run a vector through a filter, allowing only the digits and periods to come through?