[英]Manipulation on levels produced by cut R function
我想對cut
R
函數產生的水平進行一些操作。 我想在我的MWE中擁有exp(Labs)
。
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
題
我如何在這里獲得exp(Labs)
?
期望的輸出
"(497.7013, 498.6976]" "(498.6976, 499.1966]" "(499.1966, 500.1960]" "(500.1960, 501.1974]" "(501.1974, 502.2008]"
編輯
基於@akrun的答案:
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]"
一個緊湊的選擇是使用gsubfn
。 我們在pattern
參數中將數字元素與點( ([0-9.]+)
)匹配,並通過首先將匹配的對象轉換為'numeric'來替換匹配的對象,取exp
和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]"
注意:這取決於我們使用的模式。
否則將避免strsplit
避免兩次調用sub
另一種選擇。 我們split
非數字元素。 輸出將是一個list
因此我們可以使用lapply/sapply
遍歷列表元素,轉換為numeric
類,並創建具有兩列的“ data.frame”。
res <- exp(as.data.frame(t(sapply(strsplit(Labs, '[^-0-9.]+'),
function(x) as.numeric(x[-1])))))
關於根據OP的代碼獲得預期的輸出。 我改變cbind
到data.frame
原代碼,以便do.call
可以使用。
res <- exp(data.frame(lower = as.numeric( sub("\\((.+),.*", "\\1", Labs) ),
upper = as.numeric( sub("[^,]*,([^]]*)\\]", "\\1", Labs) )))
我們paste
'res'( do.call(paste0
)的行元素,然后用sprintf
或其他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]"
使用“ 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.