2014-11-06 5 views
0

R을 사용하여 CRAN cluster 패키지를 사용하여 k- 메도 이달 클러스터링 분석을 수행했습니다. 데이터는 data.frame이고 df4는 13111obs입니다. 이진수와 서수 값은 11 개입니다. 클러스터링 후 클러스터 결과를 원래 data.frame에 적용하여 해당 클러스터 번호를 사용자 ID에 표시했습니다.클러스터에 따라 서수 및 이진 데이터 집계

클러스터에 따라 이진 및 서수 선택을 어떻게 집계합니까? 예를 들어

Gender 변수 "18 ~ 20", "21 ~ 24", "25 ~ 34"남자/여자 Age 값 범위를 가지며, "35 ~ 44", "45 ~ 54", " 55-64 "및"65+ "입니다. 변수 Gender 및 범주 Age의 클러스터 당 남녀 값의 합계를 입력하십시오.

클러스터 레이블이있는 my data.frame의 머리글은 다음과 같습니다.

#12 variables because I added the clustering object to the data.frame 
#I only included two variables from the R output 
> str(df4) 
'data.frame': 13111 obs. of 12 variables: 
$ Age     : Factor w/ 7 levels "18-20","21-24",..: 6 6 6 6 7 6 5 7 6 3 ... 
$ Gender   : Factor w/ 2 levels "Female","Male": 1 1 2 2 2 1 2 1 2 2 … 

#I only included three variables from the R output 
> head(df4) 
    Age Gender 
1 55-64 Female   
2 55-64 Female   
3 55-64 Male   
4 55-64 Male   
5  65+ Male   
6 55-64 Female   

여기 내 데이터 세트와 유사한 재현 예입니다

(가상)의 출력 53,691,363,210
age <- c("18-20", "21-24", "25-34", "35-44", "45-54", "55-64", "65+") 
gender <- c("Female", "Female", "Male", "Male", "Male", "Male", "Female") 
smalldf <- data.frame(age, gender) 
#Import cluster package 
library(cluster) 
#Create dissimilarity matrix 
#Gower coefficient for finding distance between mixed variable 
smalldaisy4 <- daisy(smalldf, metric = "gower", 
        type = list(symm = c(2), ordratio = c(1))) 
#Set randomization seed 
set.seed(1) 
#Pam algorithm with 3 clusters 
smallk4answers <- pam(smalldaisy4, 3, diss = TRUE) 
#Apply cluster IDs to original data frame 
smalldf$cluster <- smallk4answers$cluster 

원하는 결과 :

cluster female male 18-20 21-24 25-34 35-44 45-54 55-64 65+ 
1 1  1  1 1  2  1  0  3  1  0 
2 2  2  1 1  1  0  1  2  0  0 
3 3  0  1 1  1  1  1  0  2  3 

내가 더 많은 정보를 제공 할 수 있는지 알려주세요.

+2

'머리 = TRUE '는 말이 안하고 파서가 질식하는 많은 "스마트 쿼트"가 있어야합니다. 또한 "정답"이라고 생각하는 것을 게시해야합니다. 특히 'with (df4, table (gender, cluster))'가 아닌 경우 –

+0

재현 가능한 코드에서 스마트 인용 부호 및

+0

@ 본드 더스트 (BondedDust) 집계에 대한 가설적인 대답을 포함 시켰고 클러스터링으로 재현 가능한 예제를 만들었습니다. –

답변

2

당신이에서 두 테이블을 표시 할 것 같습니다 클러스터별로 성별, 하나의 매트릭스에서 클러스터 별 연령 tabluation :

with(smalldf, cbind(table(cluster, gender), table(cluster, age) )) 
#---------------- 
    Female Male 18-20 21-24 25-34 35-44 45-54 55-64 65+ 
1  2 0  1  1  0  0  0  0 0 
2  0 4  0  0  1  1  1  1 0 
3  1 0  0  0  0  0  0  0 1 
+0

완벽한 작품! –