如果您想要确切的中断值,那么您应该自己计算它们,导致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
计算:
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