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
CodePudding user response:
We can try using str_extract
here for a regex option:
x <- c("c(`Kruskal-Wallis chi-squared` = 201.760850624131)",
"c(df = 17)",
"1.26686891197831e-33",
"Kruskal-Wallis rank sum test",
"delta_Z by criteria"
)
output <- as.numeric(na.omit(str_extract(x, "\\d (?:\\.\\d )?(?:e[ -]\\d )?")))
output
[1] 2.017609e 02 1.700000e 01 1.266869e-33
Explanation of regex:
\\d
match an integer component(?:\\.\\d )?
optional decimal component(?:e[ -]\\d )?
optional exponent (either positive or negative)
CodePudding user response:
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"
)
CodePudding user response:
Since it appears to be R code:
x <- c("c(`Kruskal-Wallis chi-squared` = 201.760850624131)", "c(df = 17)",
"1.26686891197831e-33", "Kruskal-Wallis rank sum test", "delta_Z by criteria")
as.character(sapply(sapply(x[1:3], str2lang, USE.NAMES = F), eval))
#> [1] "201.760850624131" "17" "1.26686891197831e-33"