剪切R功能产生的水平的操纵 [英] Manipulation on levels produced by cut R function

查看:126
本文介绍了剪切R功能产生的水平的操纵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对 cut R 函数产生的水平进行一些操作。我想在我的MWE中拥有 exp(Labs)

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

问题

如何在这里获得 exp(实验室)

期望输出

"(497.7013, 498.6976]"  "(498.6976, 499.1966]" "(499.1966, 500.1960]" "(500.1960, 501.1974]"  "(501.1974, 502.2008]" 

已编辑

基于@akrun答案:

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]"


推荐答案

一个紧凑的选择是使用 gsubfn 。我们在 pattern 参数中将数字元素与点(([0-9。] +))匹配,并且我们先将匹配的内容转换为数字,然后替换 exp round 来替换匹配的内容。

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]"

注意:这取决于

另一种选择是避免两次调用 sub 将是 strsplit 。我们将 split 放在非数字元素上,输出将是 list ,因此我们可以使用 lapply / sapply 遍历列表元素,转换为数字类,并创建一个包含两列的 data.frame。

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])))))






关于根据OP的代码获得预期的输出。将 cbind 更改为 data.frame原始代码中的,以便可以使用 do.call


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) )))

我们粘贴'res'的行元素( do.call(paste0 ),然后在其他括号中添加 sprintf 或其他 paste

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]"



更新



使用 Labs1检查输出

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

这篇关于剪切R功能产生的水平的操纵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆