-1

I need to create a function in R that takes in a string:

f3("€1.220,36 €3.002,18")

and spits out:

1220.36 3002.18

Thanks.

Eric Fail
  • 8,191
  • 8
  • 72
  • 128
skatofia
  • 17
  • 4

2 Answers2

3

We can do this with two gsubs

gsub(",", ".", gsub("\x80|\\.", "", str1))
#[1] "1220.36 3002.18"

If we need to convert to numeric, wrap it with scan

scan(text=gsub(",", ".", gsub("\x80|\\.", "", str1)), what = numeric(), quiet = TRUE)
#[1] 1220.36 3002.18

data

str1 <- "€1.220,36 €3.002,18"
akrun
  • 874,273
  • 37
  • 540
  • 662
2

Another option using the stringr library:

 library(stringr)
 x <- "€1.220,36 €3.002,18"
 str_replace_all(x, c("€" = "", "\\." = "", "\\," = "."))

 [1] "1220.36 3002.18"
wtrs
  • 319
  • 3
  • 12