如何从级别中减去数字

发布于 2024-09-04 20:26:12 字数 488 浏览 12 评论 0原文

我想将一个值范围为 0-70 的向量剪切为 x 个类别,并且想要每个类别的上限。到目前为止,我已经使用 cut() 尝试过此操作,并尝试从级别中提取限制。 我有一个级别列表,我想从中提取每个级别的第二个数字。如何提取 space 和 ] 之间的值(这是我感兴趣的数字)?

我有:

> levels(bins)
 [1] "(-0.07,6.94]" "(6.94,14]"    "(14,21]"      "(21,28]"      "(28,35]"     
 [6] "(35,42]"      "(42,49]"      "(49,56]"      "(56,63.1]"    "(63.1,70.1]" 

并且想得到:

[1] 6.94 14 21 28 35 42 49 56 63.1 70.1

或者有更好的方法来计算类别的上限吗?

I would like to cut a vector of values ranging 0-70 to x number of categories, and would like the upper limit of each category. So far, I have tried this using cut() and am trying to extract the limits from levels.
I have a list of levels, from which I would like to extract the second number from each level. How can I extract the values between space and ] (which is the number I'm interested in)?

I have:

> levels(bins)
 [1] "(-0.07,6.94]" "(6.94,14]"    "(14,21]"      "(21,28]"      "(28,35]"     
 [6] "(35,42]"      "(42,49]"      "(49,56]"      "(56,63.1]"    "(63.1,70.1]" 

and would like to get:

[1] 6.94 14 21 28 35 42 49 56 63.1 70.1

Or is there a better way of calculating the upper bounds of categories?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深海里的那抹蓝 2024-09-11 20:26:12

这可能是一种解决

k <- sub("^.*\\,","", levels(bins))
as.numeric(substr(k,1,nchar(k)-1))

方案

 [1]  6.94 14.00 21.00 28.00 35.00 42.00 49.00 56.00 63.10 70.10

This could be one solution

k <- sub("^.*\\,","", levels(bins))
as.numeric(substr(k,1,nchar(k)-1))

gives

 [1]  6.94 14.00 21.00 28.00 35.00 42.00 49.00 56.00 63.10 70.10
埖埖迣鎅 2024-09-11 20:26:12

如果您想要精确的中断值,那么您应该自己计算它们,导致间隔的 cut 轮限制:

x <- seq(0,1,by=.023)
levels(cut(x, 4))
# [1] "(-0.000989,0.247]" "(0.247,0.494]"     "(0.494,0.742]"     "(0.742,0.99]"     
levels(cut(x, 4, dig.lab=10))
# [1] "(-0.000989,0.2467555]" "(0.2467555,0.4945]"    "(0.4945,0.7422445]"   
# [4] "(0.7422445,0.989989]" 

您可以查看代码以 cut.default 如何 breaks< /code> 是计算:

if (length(breaks) == 1L) {
    if (is.na(breaks) | breaks < 2L) 
        stop("invalid number of intervals")
    nb <- as.integer(breaks + 1)
    dx <- diff(rx <- range(x, na.rm = TRUE))
    if (dx == 0) 
        dx <- abs(rx[1L])
    breaks <- seq.int(rx[1L] - dx/1000, rx[2L] + dx/1000, 
        length.out = nb)
}

简单的方法就是获取此代码并将其放入函数中:

compute_breaks <- function(x, breaks) 
    if (length(breaks) == 1L) {
        if (is.na(breaks) | breaks < 2L) 
            stop("invalid number of intervals")
        nb <- as.integer(breaks + 1)
        dx <- diff(rx <- range(x, na.rm = TRUE))
        if (dx == 0) 
            dx <- abs(rx[1L])
        breaks <- seq.int(rx[1L] - dx/1000, rx[2L] + dx/1000, 
            length.out = nb)
            breaks
    }

结果是

compute_breaks(x,4)
# [1] -0.000989  0.246755  0.494500  0.742244  0.989989

If you want exact values of breaks then you should compute them yourself, cause cut round limits for interval:

x <- seq(0,1,by=.023)
levels(cut(x, 4))
# [1] "(-0.000989,0.247]" "(0.247,0.494]"     "(0.494,0.742]"     "(0.742,0.99]"     
levels(cut(x, 4, dig.lab=10))
# [1] "(-0.000989,0.2467555]" "(0.2467555,0.4945]"    "(0.4945,0.7422445]"   
# [4] "(0.7422445,0.989989]" 

You could look on code to cut.default how breaks are compute:

if (length(breaks) == 1L) {
    if (is.na(breaks) | breaks < 2L) 
        stop("invalid number of intervals")
    nb <- as.integer(breaks + 1)
    dx <- diff(rx <- range(x, na.rm = TRUE))
    if (dx == 0) 
        dx <- abs(rx[1L])
    breaks <- seq.int(rx[1L] - dx/1000, rx[2L] + dx/1000, 
        length.out = nb)
}

So easy way is to grab this code and put into a function:

compute_breaks <- function(x, breaks) 
    if (length(breaks) == 1L) {
        if (is.na(breaks) | breaks < 2L) 
            stop("invalid number of intervals")
        nb <- as.integer(breaks + 1)
        dx <- diff(rx <- range(x, na.rm = TRUE))
        if (dx == 0) 
            dx <- abs(rx[1L])
        breaks <- seq.int(rx[1L] - dx/1000, rx[2L] + dx/1000, 
            length.out = nb)
            breaks
    }

Result is

compute_breaks(x,4)
# [1] -0.000989  0.246755  0.494500  0.742244  0.989989
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文