这个问题在这里已经有了答案:
Extracting breakpoints with intervals closed on the left
(1 个回答)
5年前关闭。
cut
的文档函数给出了“一种提取断点的方法”
aaa <- c(1,2,3,4,5,2,3,4,5,6,7)
labs <- levels(cut(aaa, 3))
cbind(lower = as.numeric( sub("\\((.+),.*", "\\1", labs) ),
upper = as.numeric( sub("[^,]*,([^]]*)\\]", "\\1", labs) ))
# lower upper
# [1,] 0.994 3.00
# [2,] 3.000 5.00
# [3,] 5.000 7.01
是否有另一种内置的方式来提取断点?
最佳答案
1) 读表 我不认为有任何直接针对此目的的内容,但这更短:
read.table(text = gsub("[^.0-9]", " ", labs), col.names = c("lower", "upper"))
给出这个 data.frame:
lower upper
1 0.994 3.00
2 3.000 5.00
3 5.000 7.01
2) gsubfn::strapply 这是另一种可能性:
library(gsubfn)
strapply(labs, "[.0-9]+", as.numeric, simplify = rbind)
给出这个矩阵:
[,1] [,2]
[1,] 0.994 3.00
[2,] 3.000 5.00
[3,] 5.000 7.01
3) gsubfn::read.pattern 另一个:
library(gsubfn)
read.pattern(text = labs, pattern = ".(.+),(.+).", col.names = c("lower", "upper"))
给予:
lower upper
1 0.994 3.00
2 3.000 5.00
3 5.000 7.01
关于r - 从 cut 中提取断点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36581075/