4

Code shows as:

readr::parse_double("123,456,789.987", 
                    locale = locale(decimal_mark = ".", 
                                    grouping_mark = ","))

where the expected result is: 123456789.987
But it turns out this throws an error:

1 parsing failure.
row # A tibble: 1 x 4 col     row   col               expected       actual 
expected   <int> <int>                  <chr>        <chr> actual 1     1    
NA no trailing characters ,456,789.987
[1] NA
attr(,"problems")

I was wondering why this happened and how to solve it?

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
Yunlong Huang
  • 85
  • 1
  • 6

1 Answers1

2

I'm not sure, but it looks like you need parse_number: from ?parse_number

The grouping mark specified by the locale is ignored inside the number.

The help page for parse_double() doesn't say it doesn't ignore the grouping mark, but it doesn't say it does ...

print(parse_number("123,456,789.987"),digits=20)
## [1] 123456789.98700000346

(The extra digits at the end occur because this number can't be exactly represented in double-precision floating point)

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453