Home > Blockchain >  How to get only numbers from a character string "../playerB/1600124.html" in R?
How to get only numbers from a character string "../playerB/1600124.html" in R?

Time:07-17

I've got the following warning when I tried to use readr::parse_number() to get numbers from the character string. How can I suppress this warning and simply get the number from the string?

> parse_number("../playerB/1600124.html")
Warning: 1 parsing failure.
row col expected                  actual
  1  -- a number ../playerB/1600124.html

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

CodePudding user response:

Another option is using str_match_all with the pattern [0-9] to get all numbers in your string like this:

string <- c("../playerB/1600124.html")
library(stringr)
string %>% 
  str_match_all("[0-9] ") %>% 
  unlist 
#> [1] "1600124"

Created on 2022-07-17 by the reprex package (v2.0.1)

CodePudding user response:

"." is considered as a decimal separator according to your locale. Change it to something else and you should get your expected output.

library(readr)

parse_number("../playerB/1600124.html", locale = locale(decimal_mark = ","))
#[1] 1600124
  • Related