问题
I want to do some manipulation on levels produced by cut
R
function. I want to have exp(Labs)
in my MWE.
set.seed(12345)
Y <- rnorm(n = 50, mean = 500, sd = 1)
Y1 <- cut(log(Y), 5)
Labs <- levels(Y1)
Labs
[1] "(6.21,6.212]" "(6.212,6.213]" "(6.213,6.215]" "(6.215,6.217]" "(6.217,6.219]"
exp(cbind(lower = as.numeric( sub("\\((.+),.*", "\\1", Labs) ),
upper = as.numeric( sub("[^,]*,([^]]*)\\]", "\\1", Labs) )))
lower upper
[1,] 497.7013 498.6976
[2,] 498.6976 499.1966
[3,] 499.1966 500.1960
[4,] 500.1960 501.1974
[5,] 501.1974 502.2008
Question
How I can get exp(Labs)
here?
Desired Output
"(497.7013, 498.6976]" "(498.6976, 499.1966]" "(499.1966, 500.1960]" "(500.1960, 501.1974]" "(501.1974, 502.2008]"
Edited
Based on @akrun answer:
Labs1 <- c("(-2.32,0.99]", "(0.99,4.28]", "(4.28,7.58]", "(7.58,10.9]", "(10.9,14.2]")
Labs1
[1] "(-2.32,0.99]" "(0.99,4.28]" "(4.28,7.58]" "(7.58,10.9]" "(10.9,14.2]"
exp(cbind(lower = as.numeric( sub("\\((.+),.*", "\\1", Labs1) ),
upper = as.numeric( sub("[^,]*,([^]]*)\\]", "\\1", Labs1) )))
lower upper
[1,] 9.827359e-02 2.691234e+00
[2,] 2.691234e+00 7.224044e+01
[3,] 7.224044e+01 1.958629e+03
[4,] 1.958629e+03 5.417636e+04
[5,] 5.417636e+04 1.468864e+06
gsubfn('([0-9.]+)', ~round(exp(as.numeric(x)),4), Labs1)
[1] "(-10.1757,2.6912]" "(2.6912,72.2404]" "(72.2404,1958.629]"
[4] "(1958.629,54176.3638]" "(54176.3638,1468864.1897]"
res1 <- exp(as.data.frame(t(sapply(strsplit(Labs1, '[^0-9.]+'),
function(x) as.numeric(x[-1])))))
sprintf('(%s]', do.call(paste, c(round(res1,4), sep=", ")))
[1] "(10.1757, 2.6912]" "(2.6912, 72.2404]" "(72.2404, 1958.629]"
[4] "(1958.629, 54176.3638]" "(54176.3638, 1468864.1897]"
回答1:
A compact option would be to use gsubfn
. We match the numeric elements with dots (([0-9.]+)
) in the pattern
argument, and we replace the matched one by first converting it to 'numeric', take the exp
and round
.
library(gsubfn)
gsubfn('([-0-9.]+)', ~round(exp(as.numeric(x)),4), Labs)
#[1] "(497.7013,498.6976]" "(498.6976,499.1966]" "(499.1966,500.196]"
#[4] "(500.196,501.1974]" "(501.1974,502.2008]"
NOTE: This depends on the pattern we use.
Or another option to avoid two calls to sub
would be strsplit
. We split
on the non-numeric elements. The output will be a list
so we can use either lapply/sapply
to loop over the list elements, convert to numeric
class, and create a 'data.frame' with two columns.
res <- exp(as.data.frame(t(sapply(strsplit(Labs, '[^-0-9.]+'),
function(x) as.numeric(x[-1])))))
Regarding getting the expected output based on the OP's code. I changed cbind
to data.frame
in the original code so that do.call
can be used.
res <- exp(data.frame(lower = as.numeric( sub("\\((.+),.*", "\\1", Labs) ),
upper = as.numeric( sub("[^,]*,([^]]*)\\]", "\\1", Labs) )))
We paste
the row elements of 'res' (do.call(paste0
) and then add the additional parentheses with either sprintf
or another paste
sprintf('(%s]', do.call(paste, c(round(res,4), sep=", ")))
#[1] "(497.7013, 498.6976]" "(498.6976, 499.1966]" "(499.1966, 500.196]"
#[4] "(500.196, 501.1974]" "(501.1974, 502.2008]"
Update
Checking the output with the 'Labs1'
gsubfn('([-0-9.]+)', ~round(exp(as.numeric(x)),4), Labs1)
#[1] "(0.0983,2.6912]" "(2.6912,72.2404]"
#[3] "(72.2404,1958.629]" "(1958.629,54176.3638]"
#[5] "(54176.3638,1468864.1897]"
exp(as.data.frame(t(sapply(strsplit(Labs1, '[^-0-9.]+'),
function(x) as.numeric(x[-1])))))
# V1 V2
#1 9.827359e-02 2.691234e+00
#2 2.691234e+00 7.224044e+01
#3 7.224044e+01 1.958629e+03
#4 1.958629e+03 5.417636e+04
#5 5.417636e+04 1.468864e+06
来源:https://stackoverflow.com/questions/31702464/manipulation-on-levels-produced-by-cut-r-function