26

I've been using gsub("toreplace","replacement", myvector) to clean out data in R. While this works for commas and the like, removing "$" has no effect. So if I do gsub("$","",myvector) all the dollar signs remain in place.

I think this is because $ is a special character in R. I tried escaping it "\$" but that yields the same result (no effect). And I couldn't find a resource on escaping special characters in R.

Obviously I should do this in preprocessing. But I was wondering if anyone out there knew how to either a) escape special characters in R b) get rid of pesky $ in R directly. For science.

Saanch
  • 1,814
  • 1
  • 24
  • 38
araneae
  • 1,525
  • 2
  • 13
  • 13

4 Answers4

30

You have to escape it twice, first for R, second for the regex.

gsub('\\$', '', c("a$a", "bb$"))
[1] "aa" "bb"

See ?Quotes for details on quoting and escaping.

SiggyF
  • 22,088
  • 8
  • 43
  • 57
21

Use fixed = TRUE:

gsub('$', '', c("a$a", "bb$"), fixed = TRUE)

Then you don't need to worry about any special characters. In stringr, this is implemented a little differently:

library(stringr)
str_replace_all(c("$100","ta$ty"), fixed("$"), "")

Thanks to DiggyF and James for the examples!

hadley
  • 102,019
  • 32
  • 183
  • 245
14

Escaping characters can be a pain some times, but just putting it in square brackets (make it a character class) helps with this:

> gsub("[$]","",c("$100","ta$ty"))
[1] "100"  "taty"
James
  • 65,548
  • 14
  • 155
  • 193
1

if you have $ followed by number in set of data columns (e.g. $400,000) there is an easier way that worked like charm for me. data%>% mutate_at(5:6, parse_number) where 5:6 are the data column numbers.