-1
> gsub('$', ' ' ,"$170,000")
[1] "$170,000 "
> gsub(',', ' ' ,"$170,000")
[1] "$170 000"

Why is the first output not " 170,000" ?

  • $ is a metacharacter and needs to be escaped. Just lke (, ^, | and others. – Roman Luštrik May 24 '21 at 11:47
  • `gsub` uses regular expressions, so `$` has a specific meaning. Well, lots of things have a specific meaning with regular expressions. For your behavior, specify `fixed = TRUE`, because then it will just grab the $. – slamballais May 24 '21 at 11:47

1 Answers1

0

Because you have to escape $:

gsub(pattern = "\\$", replacement = " ", x = "$170,000")

" 170,000"

For more info about escape: click here

bird
  • 2,938
  • 1
  • 6
  • 27