Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: parse_number fails if the string contains a dot

Tags:

r

readr

parse_number from readr fails if the character string contains a . It works well with special characters.

library(readr)

#works
parse_number("%ç*%&23")

#does not work
parse_number("art. 23")

Warning: 1 parsing failure.
row col expected actual
  1  -- a number      .

[1] NA
attr(,"problems")
# A tibble: 1 x 4
    row   col expected actual
  <int> <int> <chr>    <chr> 
1     1    NA a number .

Why is this happening?

Update:

The excpected result would be 23

like image 807
captcoma Avatar asked Dec 08 '25 17:12

captcoma


1 Answers

There is a space in after the dot which is causing an error. What is the expected number from this sequence (0.23 or 23)?

parse_number seems to look for decimal and grouping separators as defined by your locale, see the documentation here https://www.rdocumentation.org/packages/readr/versions/1.3.1/topics/parse_number

You can opt to change the locale using the following (grouping_mark is a dot with a space):

parse_number("art. 23",  locale=locale(grouping_mark=". ", decimal_mark=","))
Output: 23

or remove the space in front:

parse_number(gsub(" ", "" , "art. 23")) 
Output: 0.23 

Edit: To handle dots as abbreviations and numbers use the following:

library(stringr)

> as.numeric(str_extract("art. 23", "\\d+\\.*\\d*"))
[1] 23
> as.numeric(str_extract("%ç*%&23", "\\d+\\.*\\d*"))
[1] 23

The above uses regular expressions to identify number patterns within strings.

  • \\d+ finds a digits
  • \\.* finds a dot
  • \\d* finds the remaining digits

Note: I am no expert on regex but there are plenty of other resources that will make you one

like image 169
Jamie_B Avatar answered Dec 11 '25 11:12

Jamie_B



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!