由于您没有提供数据,我使用了
kmeans
文档,它为具有两列x和y的数据生成两组。您可以
split
将原始数据按每行所属的集群,然后从每个组中提取数据。我不确定我的示例数据是否与您的数据相似,但在下面的代码中,我只是使用了
min
x列值和
max
列y的值作为潜在间隔的边界(取决于用例,这是否合理)。这对你有帮助吗?
data <- rbind(matrix(rnorm(100, sd = 0.3), ncol = 2),
matrix(rnorm(100, mean = 1, sd = 0.3), ncol = 2))
colnames(data) <- c("x", "y")
cl <- kmeans(data, 2)
data <- as.data.frame(cbind(data, cluster = cl$cluster))
lapply(split(data, data$cluster), function(x) {
min_x <- min(x$x)
max_y <- max(x$y)
diff <- max_y-min_x
c(min_x = min_x , max_y = max_y, diff = diff)
})
# $`1`
# min_x max_y diff
# -0.6906124 0.5123950 1.2030074
#
# $`2`
# min_x max_y diff
# 0.2052112 1.6941800 1.4889688