시작하기무료로 시작하기

계층적 클러스터링: 탐색 준비

이제 oes 데이터에 대한 잠재적 클러스터링을 만들었습니다. ggplot2로 이 클러스터를 탐색하려면, 각 직업에 클러스터를 할당한 뒤 oes 데이터 행렬을 정돈된 데이터 프레임으로 변환해야 합니다.

이 연습은 강의의 일부입니다

R로 배우는 군집 분석

강의 보기

연습 안내

  • tibble 라이브러리의 rownames_to_column()을 사용해 행 이름을 열로 보존하면서 oes data.matrix로부터 df_oes 데이터 프레임을 만드세요.
  • cutree()를 사용해 h = 100,000으로 클러스터 할당 벡터 cut_oes를 만드세요.
  • 클러스터 할당을 cluster 열로 df_oes 데이터 프레임에 추가하고, 결과를 clust_oes라는 새 데이터 프레임으로 저장하세요.
  • tidyr() 라이브러리의 pivot_longer() 함수를 사용해 ggplot2 분석에 적합한 형식으로 데이터를 재구조화하고, 정돈된 데이터 프레임을 gathered_oes로 저장하세요.

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

dist_oes <- dist(oes, method = 'euclidean')
hc_oes <- hclust(dist_oes, method = 'average')

library(tibble)
library(tidyr)

# Use rownames_to_column to move the rownames into a column of the data frame
df_oes <- rownames_to_column(as.data.frame(___), var = 'occupation')

# Create a cluster assignment vector at h = 100,000
cut_oes <- cutree(___, h = ___)

# Generate the segmented oes data frame
clust_oes <- mutate(___, cluster = ___)

# Create a tidy data frame by gathering the year and values into two columns
gathered_oes <- pivot_longer(data = ___, 
                       cols = -c(occupation, cluster),
                       names_to = "year",               
                       values_to = "mean_salary" )
코드 편집 및 실행