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.
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.
We can do this with two gsub
s
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
str1 <- "€1.220,36 €3.002,18"
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"