我如何知道每个簇中的元素数量?

How do I know the number of elements in each cluster?

我想知道如何找出每个簇中的元素数量。我在下面插入了一个可执行代码。从图中我知道集群 1 有 8 个属性,集群 2 有 4 个属性,集群 3 有 7 个属性。但是如果我有 200 个属性,例如,看情节就会很混乱。有没有更直接的功能可以做到这一点?

library(ggplot2)
library(rdist)
library(geosphere)


df<-structure(list(Propertie = c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19), Latitude = c(-23.8, -23.9, -23.5, -23.4, -23.6,-23.9, -23.2, -23.5, -23.8, -23.7, -23.8, -23.9, -23.4, -23.9, 
                                                                                                 -23.9, -23.2, -23.3, -23.7, -23.8), 
                    Longitude = c(-49.1, -49.3,-49.4, -49.7, -49.6, -49.6, -49.6, -49.6, -49.6, -49.6, -49.7,-49.2, -49.5, -49.8, -49.5, -49.3, -49.3, -49.2, -49.5), 
                    Waste = c(526,350, 526, 469, 285, 175, 175, 350, 350, 175, 350, 175, 175, 364,175, 175, 350, 45.5, 54.6)), 
               class = "data.frame", row.names = c(NA, -19L)) 

#clusters
coordinates<-df[c("Latitude","Longitude")]
d<-as.dist(distm(coordinates[,2:1]))
fit.average<-hclust(d,method="average") 
clusters<-cutree(fit.average, k=3) 
df$cluster <- clusters 
plot(fit.average,hang=-1,cex=.8,main="Average Linkage Clustering")
rect.hclust(fit.average,k=3)

试试这个:

table(clusters)

如果要提取向量中的值

as.numeric(table(clusters))

当你使用cutree函数时,这会生成一个整数向量标记元素1n,其中n是你设置的簇数。 table函数统计属于每个簇的元素