R 将分类/群集任务中的文本值更改为int

R 将分类/群集任务中的文本值更改为int,r,dataframe,cluster-analysis,data-manipulation,R,Dataframe,Cluster Analysis,Data Manipulation,任务是使用dataset在R中进行聚类,其中包含分类文本值(如“蓝色”、“红色”)。虽然k-means算法只接受数值,所以我需要将文本类别转换为int类别。有什么单线解决方案吗?或者其他什么 Attribute1 Attribute2 Attribute1 Attribute2 Instance1 "blue" "red" => Instance1 1 2 Instance2

任务是使用dataset在R中进行聚类,其中包含分类文本值(如“蓝色”、“红色”)。虽然k-means算法只接受数值,所以我需要将文本类别转换为int类别。有什么单线解决方案吗?或者其他什么

         Attribute1 Attribute2                     Attribute1 Attribute2
Instance1  "blue"     "red"         =>  Instance1       1          2
Instance2  "green"    "blue"            Instance2       3          1

a[,1]因此,在进行聚类分析时,我们希望使我们的聚类尽可能彼此远离。我所考虑的有问题的方法
太过直截了当,效果会很差(如果实现的话)。好的解决方案是应用一些可以处理混合数据类型的距离度量。
评论中提到的是gower,它是通用的。二元类别也存在Jaccard距离。
高尔解决方案(R)的示例:

库(集群)

gower.dissimility.mtrx你不能这样做,如果你将“蓝色”、“红色”转换为1,2,你是在暗示某种顺序,我猜不是这样的?你应该做一个热编码,或者尝试gower距离进行聚类,例如,是的,非常感谢。这正是我所期待的,它帮助了我:)
x_num <- as.numeric(x)
library(cluster)
gower.dissimilarity.mtrx <- daisy(DataFrame, metric = c("gower"))