Convert values to numeric handling multiple decimal separators
Source:R/safe_to_numeric.R
safe_to_numeric.RdAttempts to convert values to numeric. If the input is already numeric, returns it as-is. Character strings are parsed with support for:
Dot-decimal (
"10.5") and comma-decimal ("10,5") notationScientific notation (
"1e-5","2.5E+10")Thousand separators when both dot and comma are present (
"1.234,56"European or"1,234.56"US)
Details
The function is vectorized: when given a character vector, each element is parsed independently.
Examples
safe_to_numeric(10.5)
#> [1] 10.5
safe_to_numeric("10.5")
#> [1] 10.5
safe_to_numeric("10,5")
#> [1] 10.5
safe_to_numeric("-3.14")
#> [1] -3.14
safe_to_numeric("1e-5")
#> [1] 1e-05
safe_to_numeric("1.234,56")
#> [1] 1234.56
safe_to_numeric(c("10.5", "10,5", "text", "1e-3"))
#> [1] 10.500 10.500 NA 0.001