Home > Software engineering >  extracting numbers from string in R using a template
extracting numbers from string in R using a template

Time:03-05

Can you help me please to write an R function that extracts two numericals from a string that looks like this

16,296 (12.4%)

The answer should be

16296 and 12.4

Thank you

CodePudding user response:

It is difficult to come up with a general solution with only one example but here is my attempt. You have also not shared how is this data stored in R environment but I'll assume this is a string.

x <- '16,296 (12.4%)'
num1 <- readr::parse_number(x)
num2 <- as.numeric(sub('.*\\((\\d \\.\\d )\\%\\).*', '\\1', x))

num1
#[1] 16296

num2
#[1] 12.4

where readr::parse_number returns the first numeric value and output of sub is a number in parenthesis ((...)).

CodePudding user response:

Another option:

x <- '16,296 (12.4%)'

library(magrittr)
#gsub to remove (, ), % and comma.
#split on space, unlisted and convert to number
gsub("[(),%]", "", x) %>% strsplit(" ") %>% unlist() %>% as.numeric()
  • Related