这些R代码的含义?它们相关吗?

Meaning of these R codes? Are they correlated?

我正在研究 R 中的鸢尾花数据集,我想对以下两个代码进行一些说明:

cluster_iris<-kmeans(iris[,1:4], centers=3)

iris$ClusterM <- as.factor(cluster_iris$cluster) 

我认为第一个是使用数据文件的所有个案执行 k-means 聚类分析,并且仅使用前 4 列并选择 3 个聚类。 但是我不确定第二段代码在做什么?第一个只是说明分析的偏好,第二个是实际执行它(即执行 k-means)吗?

感谢任何帮助

第一行进行聚类分析,并将聚类标签存储在名为 cluster_iris$cluster 的组件中,它只是一个数字向量。

第二行将该聚类编号作为分类标签放到原始数据集的行上。所以现在你的鸢尾花数据在名为 "ClusterM".

的列中包含了所有的花瓣和萼片以及一个簇索引
> head(iris)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species ClusterM
1          5.1         3.5          1.4         0.2  setosa        1
2          4.9         3.0          1.4         0.2  setosa        3
3          4.7         3.2          1.3         0.2  setosa        3
4          4.6         3.1          1.5         0.2  setosa        3