-2

I have a csv file, where the prices after exported in R as a list, are worded like: $115.00 USD, so I cannot convert it with

as.numeric(sub('\\$','',as.character(Data))) 

because it returns with N/As because of the " USD" part.

Cœur
  • 37,241
  • 25
  • 195
  • 267
fokos
  • 1

1 Answers1

0

It's easiest to just remove everything that isn't a number or period:

> gsub("[^0-9.]", "", "$115.00 USD")
[1] "115.00"

If you want to ensure that you have a contiguous string of characters from the input, you can use a capture:

> sub("[^0-9.]*([0-9.]*).*", "\\1", "$115.00 USD")
[1] "115.00"
Matthew Lundberg
  • 42,009
  • 6
  • 90
  • 112