Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

readr::parse_number with leading zero

I would like to parse numbers that have a leading zero.

I tried readr::parse_number, however, it omits the leading zero.

library(readr)

parse_number("thankyouverymuch02")
#> [1] 2

Created on 2022-12-30 with reprex v2.0.2

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The desired output would be 02

>Solution :

The simplest and most naive would be:

gsub("\\D", "", "thankyouverymuch02")
[1] "02"

The regex special "\\d" matches a single 0-9 character only; the inverse is "\\D" which matches a single character that is anything except 0-9.

If you have strings with multiple patches of numbers and you want them to be distinct, neither parse_number nor this simple gsub is going to work.

gsub("\\D", "", vec)
# [1] "02"   "0302"

For that, it must always return a list (since we don’t necessarily know a priori how may elements have 0, 1 or more number-groups).

vec <- c("thankyouverymuch02", "thank03youverymuch02")
regmatches(vec, gregexpr("\\d+", vec))
# [[1]]
# [1] "02"
# [[2]]
# [1] "03" "02"

#### equivalently
stringr::str_extract_all(vec, "\\d+")
# [[1]]
# [1] "02"
# [[2]]
# [1] "03" "02"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading