1) 우선 cluster 패키지를 불러와서 라이브러리 선언
install.packages("cluster")
library(cluster)
2) 거리 데이터를 행렬로 입력
d<-matrix(c(
0, 1, 11, 5,
1, 0, 2, 3,
11, 2, 0, 4,
5, 3, 4, 0), nrow=4, ncol=4, byrow=T)
3) 거리 데이터를 입력한 행렬을 거리행렬로 지정하기 위해서 as.dist() 옵션을 사용
d<-as.dist(d)
4) 계보적 군집분석을 위한 hclust(d, method="방법") 함수를 사용하려면 거리행렬(d)만 사용가능
hclust(d, method="single") # single linkage
hclust(d, method="complete") # complete linkage
hclust(d, method="average") # average linkage
5) dendrogram으로 보려면 plot()함수 사용
plot(hclust(d, method="single"))
plot(hclust(d, method="complete"))
plot(hclust(d, method="average"))
'통계 > R' 카테고리의 다른 글
Mac에서 RStudio에서 txt파일 불러올 때 한글 깨지는 문제 해결 in R (1) | 2017.09.05 |
---|---|
princomp함수를 이용한 주성분분석 (0) | 2016.11.07 |
[R] PCA 주성분분석 5가지 함수 (0) | 2016.11.07 |
[R] data frame에서 행 랜덤표본추출 방법 (0) | 2016.11.07 |
R studio 단축키 (0) | 2016.10.09 |