Reputation: 13401
I have a character vector and want to fetch a numbers from it both integer as well as Floats.
a <- c("Sam is 5 years old", "Ram is 3.7 years old" , "John is 17 years 2 months old")
output should be:
[1] 5 3.7 17.2
Upvotes: 3
Views: 126
Reputation: 51592
Here is a one liner,
as.numeric(gsub(' ', '.', trimws(gsub('\\D+', ' ', a))))
#[1] 5.0 3.7 17.2
Upvotes: 3
Reputation: 887128
We can use parse_number
from readr
readr::parse_number(a)
#[1] 5.0 3.7 17.0
Based on the OP's new example
library(stringr)
sapply(str_extract_all(a, "[0-9]+\\s+(years|months)"), function(x) {
x1 <- readr::parse_number(x)
head(if(length(x1)==2) x1 + round(x1[2]/12, 1) else x1, 1)})
#[1] 5.0 7.0 17.2
If we don't need to be concerned about dividing by 12 for the 'months, another option is
as.numeric(sapply(regmatches(a, gregexpr('[0-9]+', a)), paste, collapse="."))
#[1] 5.0 3.7 17.2
Upvotes: 4