Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing Two Characters From A String

Tags:

r

stringr

Related question here.

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

This removes dollar signs =

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

This removes commas =

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

I'm wondering if I could remove both characters in one step.

I realize that I could just save the gsub results into a new variable, and then reapply that (or another function) on that variable. But I guess I'm wondering about a single step to do both.

like image 289
ATMathew Avatar asked Jul 04 '12 22:07

ATMathew


Video Answer


1 Answers

take a look at ?regexp for additional special regex notation:

> gsub('[[:punct:]]', '', d)
[1] "000"     "1059890" "1308247"
like image 70
Justin Avatar answered Oct 12 '22 03:10

Justin