Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
根据R中的聚类聚合序号和二进制数据_R_Aggregate_Cluster Analysis_R Daisy - Fatal编程技术网

根据R中的聚类聚合序号和二进制数据

根据R中的聚类聚合序号和二进制数据,r,aggregate,cluster-analysis,r-daisy,R,Aggregate,Cluster Analysis,R Daisy,我使用带有R的CRAN cluster包进行k-medoid聚类分析。数据位于一个名为df4的data.frame上,带有13111 obs。由11个二进制和序数值组成。聚类后,我将聚类结果应用于原始的数据。frame显示了与用户id对应的聚类编号 如何根据集群聚合二进制和顺序选择? 例如,Gender变量具有男性/女性值,Age范围为“18-20”、“21-24”、“25-34”、“35-44”、“45-54”、“55-64”和“65+”。我想要变量Gender和Age中的类别的每个集群的男性

我使用带有R的
CRAN cluster
包进行k-medoid聚类分析。数据位于一个名为df4的
data.frame
上,带有13111 obs。由11个二进制和序数值组成。聚类后,我将聚类结果应用于原始的
数据。frame
显示了与用户id对应的聚类编号

如何根据集群聚合二进制和顺序选择?

例如,
Gender
变量具有男性/女性值,
Age
范围为“18-20”、“21-24”、“25-34”、“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           
下面是一个类似于我的数据集的可复制示例:

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

如果我能提供更多信息,请告诉我。

看起来您希望在一个矩阵中显示“按性别分组”和“按年龄分组”表格中的两个表格:

 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

head=TRUE
没有任何意义,而且你有很多“智能引号”会导致解析器阻塞。你还应该发布你认为是“正确答案”的内容,特别是如果它不只是使用(df4,table(gender,cluster))我已经删除了智能引号,@BondedDust我为聚合添加了一个假设答案,并制作了一个可复制的集群示例。@MichaelDavidson我一直从键盘上忘记这些引号是不起作用的。谢谢!这不是你的键盘,而是你的编辑器。
 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