Extracting Numeric Values in R (from a Free text)

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Extracting Numeric Values in R (from a Free text)

BalajiS R User
I am trying to extract the age(numeric values) from a vector , tryied the following but dint worked

Example:-

    a <- "age-32 agge32 age 45 aage 56 agee 34"
    b <- gsub("[Aa]g.e?"," age ",a)
    sa <- strsplit(x=b, split="\\s")
    age <- stringr::str_extract(sa, "(age?)(\\s[^\\s]+){1}")
     (or)
    age <- grep("(([^\\s]+\\s){0}(age)(\\s[^\\s]+){1})",sel,value = TRUE)


Output Expect is 32 32 45 56 34