The documentation of the cut
function gives "one way to extract the breakpoints"
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
Is there another - build-in - way to extract the breakpoints?
1) read.table I don't think there is anything directly intended for this but this is shorter:
read.table(text = gsub("[^.0-9]", " ", labs), col.names = c("lower", "upper"))
giving this data.frame:
lower upper
1 0.994 3.00
2 3.000 5.00
3 5.000 7.01
2) gsubfn::strapply and this is another possibility:
library(gsubfn)
strapply(labs, "[.0-9]+", as.numeric, simplify = rbind)
giving this matrix:
[,1] [,2]
[1,] 0.994 3.00
[2,] 3.000 5.00
[3,] 5.000 7.01
3) gsubfn::read.pattern and another:
library(gsubfn)
read.pattern(text = labs, pattern = ".(.+),(.+).", col.names = c("lower", "upper"))
giving:
lower upper
1 0.994 3.00
2 3.000 5.00
3 5.000 7.01
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With