简体   繁体   中英

R Cluster with grouped data

I'm new to R. I have the following data table:

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). 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:

d<-dist(yourmatrix[,4])
I suppose that your 4th column has values:166.834,131.567 and so on.  

2.Build the clusters:

hc<-hclust(d)  

3.finally,plot your dendrogram:

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')

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM