我救了你表myData
:
myData
artikel naam product personeel loon verlof
doc1 1 1 2 1 0 0
doc2 1 1 1 0 0 0
doc3 0 0 1 1 2 1
doc4 0 0 0 1 1 1
然后从e1071
库使用hamming.distance()
功能。您可以使用自己的距离(只要它们在矩阵形式)
lilbrary(e1071)
distMat <- hamming.distance(myData)
使用“完整”的联动方式,以确保一个集群内的最大距离可在以后指定层次聚类紧随其后。根据一组中的点之间的最大距离
dendrogram <- hclust(as.dist(distMat), method="complete")
选择组(最大= 5)
groups <- cutree(dendrogram, h=5)
最后绘制的结果:
plot(dendrogram) # main plot
points(c(-100, 100), c(5,5), col="red", type="l", lty=2) # add cutting line
rect.hclust(dendrogram, h=5, border=c(1:length(unique(groups)))+1) # draw rectangles

的另一种方式查看每个文档的群集成员身份是table
:
table(groups, rownames(myData))
groups doc1 doc2 doc3 doc4
1 1 1 0 0
2 0 0 1 1
所以文件第一和第二落入一个组,而第三和第四 - 另一组。
如果您包含可重复的示例,则此问题将更容易回答,并且对其他人更有用。请参阅https://stackoverflow.com/help/how-to-ask和http://stackoverflow.com/q/5963269/134830 – 2014-10-27 10:10:50