2017-05-24 17 views
0

我一直在做R中的一些分层聚类。直到现在它的工作状况良好,产生了hclust对象左和中心,但突然不再。现在,在执行时,它只会产生列表:分层聚类产生列表而不是hclust

mydata.clusters <- hclust(dist(mydata[, 1:8])) 
mydata.clustercut <- cutree(mydata.clusters, 4) 

,并试图在:

table(mydata.clustercut, mydata$customer_lifetime) 

它不产生一个表,但值(IM从列表中猜测)无尽的打印。

+3

欢迎!#2你可以看这里[如何发布一个可重现的例子](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example),并确保您的例子可重现。您无需将整个数据集发布为复制问题的子集。 – OdeToMyFiddle

回答

0

cutree函数提供每个观察所属的分组。例如:

iris.clust <- hclust(dist(iris[,1:4])) 
iris.clustcut <- cutree(iris.clust, 4) 

iris.clustcut 
# [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 
# [52] 2 2 3 2 3 2 3 2 3 3 3 3 2 3 2 3 3 2 3 2 3 2 2 2 2 2 2 2 3 3 3 3 2 3 2 2 2 3 3 3 2 3 3 3 3 3 2 3 3 2 2 
# [103] 4 2 2 4 3 4 2 4 2 2 2 2 2 2 2 4 4 2 2 2 4 2 2 4 2 2 2 4 4 4 2 2 2 4 2 2 2 2 2 2 2 2 2 2 2 2 2 2 

附加的比较然后可以用这个作为观测数据分组变量来实现:

new.iris <- data.frame(iris, gp=iris.clustcut) 

# example to visualise quickly the Species membership of each group 

library(ggplot2) 
ggplot(new.iris, aes(gp, fill=Species)) + 
    geom_bar() 

enter image description here

+0

对我来说,这可以推算出每个群集中有多少人的情节。无论我尝试“填充”哪种变异。 – Ichibichi