Removing two characters from a string

The next question is here .

So, I have a character vector with currency values โ€‹โ€‹that contain dollar signs and commas. However, I want to try to remove both the comma and dollar signs in the same step.

This removes the dollar signs =

d = c("$0.00", "$10,598.90", "$13,082.47") gsub('\\$', '', d) 

This removes the commas =

 library(stringr) str_replace_all(c("10,0","tat,y"), fixed(c(","), "") 

I am wondering if I can remove both characters in one step.

I understand that I can just save the gsub results to a new variable and then reapply this (or another function) to this variable. But I think I'm wondering how to take the same step.

+8
r stringr
source share
2 answers

Since the answer in the comments is bad:

 gsub('\\$|,', '', d) 

replaces either $ or ( | ) , an empty string.

+12
source share

take a look at ?regexp for an extra special regex entry:

 > gsub('[[:punct:]]', '', d) [1] "000" "1059890" "1308247" 
+3
source share

All Articles