Search code examples
rstringextractstringr

Extract digits with decimal from a vector R


I have the following vector:

c("c(`Kruskal-Wallis chi-squared` = 201.760850624131)", "c(df = 17)", 
"1.26686891197831e-33", "Kruskal-Wallis rank sum test", "delta_Z by criteria"
)

I desired this output:

c("201.760850624131", "17", "1.26686891197831e-33")

Thanks for any help


Solution

  • We can use str_extract with the regex that matches one or more digits ([0-9]+) followed by a . then one or more digits and e followed by - or + and any digits

    library(stringr)
    as.numeric(na.omit(str_extract(v1, "[0-9]+(\\.[0-9]+e[-+]\\d+)?")))
    

    -output

    [1] 2.010000e+02 1.700000e+01 1.266869e-33
    

    data

    v1 <- c("c(`Kruskal-Wallis chi-squared` = 201.760850624131)", "c(df = 17)", 
    "1.26686891197831e-33", "Kruskal-Wallis rank sum test", "delta_Z by criteria"
    )