[英]R Cluster with grouped data
I'm new to R. I have the following data table: 我是R.的新手。我有以下数据表:
22 1045 4 166.834
23 1047 2 131.567
26 1054 2 43.583
28 1057 2 280.184
34 1064 1 134.608
44 1081 1 70.862
47 1086 2 27.156
50 1094 2 259.189
51 1096 4 216.103
56 1109 2 429.679
59 1115 1 76.770
The table is already grouped by the third column. 该表已按第三列分组。 What I'm tyring to do is to plot a Matrix which shows that there are 3 Cluster for example (1,2-3,4). 我要做的就是绘制一个Matrix,它表明有3个Cluster(例如1,2-3,4)。 Is there an easy way to do it? 有一个简单的方法吗?
Thanks in advance 提前致谢
There is one way,as for me, to build dendrogram. 对我来说,有一种方法可以构建树状图。
1.Build the distance matrix: 1.建立距离矩阵:
d<-dist(yourmatrix[,4])
I suppose that your 4th column has values:166.834,131.567 and so on.
2.Build the clusters: 2.建立集群:
hc<-hclust(d)
3.finally,plot your dendrogram: 3.最后,绘制你的树状图:
plot(hc)
For your data 对于您的数据
head(df)
x y cluster z
1 22 1045 4 166.834
2 23 1047 2 131.567
3 26 1054 2 43.583
4 28 1057 2 280.184
5 34 1064 1 134.608
6 44 1081 1 70.862
you can visualize like the following: 您可以像下面这样形象化:
library(scatterplot3d)
scatterplot3d(df$x, df$y, df$z, color=df$cluster, pch=19, xlab='x', ylab='y', zlab = 'z')
legend("topright", legend=1:4, pch=19, col=1:4, title='cluster')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.