对切​​割R函数产生的水平进行操作

时间:2015-07-29 13:51:26

标签: r cut

我想对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]"

1 个答案:

答案 0 :(得分:1)

紧凑的选项是使用gsubfn。我们将([0-9.]+)参数中的数字元素与点(pattern)进行匹配,然后我们将匹配的元素替换为首先将其转换为“数字”,取exp和{{1} }。

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'。

numeric

关于根据OP的代码获得预期输出。我在原始代码中将res <- exp(as.data.frame(t(sapply(strsplit(Labs, '[^-0-9.]+'), function(x) as.numeric(x[-1]))))) 更改为cbind,以便可以使用data.frame

do.call

我们 res <- exp(data.frame(lower = as.numeric( sub("\\((.+),.*", "\\1", Labs) ), upper = as.numeric( sub("[^,]*,([^]]*)\\]", "\\1", Labs) ))) 'res'(paste)的行元素,然后使用do.call(paste0或其他sprintf

添加其他括号
paste

更新

使用'Labs1'检查输出

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