N <- 100; x <- sort(runif(N))
cp <- cutpoints(x, 6, 0.9)
n <- length(cp$cutp)
# Print out
nocp <- rle(findInterval(x, c(-Inf, cp$cutp, Inf)))$lengths
cbind(c(-Inf, cp$cutp), c(cp$cutp, Inf), nocp)
# Define a factor from the cutting points
fx <- cut(x, breaks = c(-Inf, cp$cutp, Inf))
# Plot points and cutting points
plot(x, rep(0, N), col="gray", ann = FALSE)
points(cp$cutp, rep(0, n), pch="|", col=2)
# Compare with k-means clustering
km <- kmeans(x, n)
points(x, rep(0, N), col = km$cluster, pch = "+")
## A 2-dimensional example
x <- y <- c()
for (i in 1:9) {
for (j in 1:9) {
x <- c(x, i + rnorm(20, 0, 0.2))
y <- c(y, j + rnorm(20, 0, 0.2))
}
}
cpx <- cutpoints(x, 8, 0)
cpy <- cutpoints(y, 8, 0)
plot(x, y, pch = 18, col=rgb(0.5,0.5,0.5), axes=FALSE, ann=FALSE)
for (xi in cpx$cutp) abline(v=xi, col=2, lty=2)
for (yi in cpy$cutp) abline(h=yi, col=2, lty=2)
km <- kmeans(cbind(x, y), 81)
points(x, y, col=km$cluster)
Run the code above in your browser using DataLab