Вопрос

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.

Это было полезно?

Решение

Since answering in the comments is bad:

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

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

Другие советы

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

> gsub('[[:punct:]]', '', d)
[1] "000"     "1059890" "1308247"
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top