当我的数据的第一列有列名时如何使用 fviz_cluster 集群可视化包
how to use the fviz_cluster cluster visualization package when the first column of my data has a column name
我正在使用 fviz_cluster 包。 https://afit-r.github.io/kmeans_clustering 上有一个很好的教程,其中展示了如何使用该包来可视化集群。这很简单。他们在教程中使用的数据是 df <- USArrests。查看数据时显示为
'data.frame': 50 obs. of 4 variables:
$ Murder : num 13.2 10 8.1 8.8 9 7.9 3.3 5.9 15.4 17.4 ...
$ Assault : int 236 263 294 190 276 204 110 238 335 211 ...
$ UrbanPop: int 58 48 80 50 91 78 77 72 80 60 ...
$ Rape : num 21.2 44.5 31 19.5 40.6 38.7 11.1 15.8 31.9 25.8 ...
具有每个观察键的数据框第一列没有列header。这样,这个包就很好用了。
我的数据显然有一列header。我应该如何使我的数据看起来像 USArrests 这样才有效,但是我需要将簇号附加回数据?
我的数据有 11 列,包括带有观察名称的第一列,所以我通过使用
跳过第一列来进行聚类
[,2:11]
when I use this to visualize using
fviz_cluster(allLfit, data = allLdf[,2:11])
它有效,但情节使用了模棱两可的名字
有什么建议吗??
谢谢!!!
structure(list(PIN = structure(1:5, .Label = c("a", "b", "c",
"d", "e"), class = "factor"), v1 = c(0.8, 0.36, 0.21, 0.84, 0.43
), v2 = c(0.87, 0.01, 0.56, 0.75, 0.98), v3 = c(0.48, 0.13, 0.26,
0.34, 0.83)), row.names = c(NA, 5L), class = "data.frame")
按照 link 中相同的步骤,scale
d 创建数字列 'df',同时将 row.names
设置为第一列,获取 kmeans
('k2') 并在 'k2' 上使用 fviz_cluster
指定 'df' 作为 scale
d 数据集
library(factoextra)
library(cluster)
df <- scale(`row.names<-`(allLdf[-1], allLdf[[1]]))
k2 <- kmeans(df, centers = 2, nstart = 25)
fviz_cluster(k2, data = df)
-输出
我正在使用 fviz_cluster 包。 https://afit-r.github.io/kmeans_clustering 上有一个很好的教程,其中展示了如何使用该包来可视化集群。这很简单。他们在教程中使用的数据是 df <- USArrests。查看数据时显示为
'data.frame': 50 obs. of 4 variables:
$ Murder : num 13.2 10 8.1 8.8 9 7.9 3.3 5.9 15.4 17.4 ...
$ Assault : int 236 263 294 190 276 204 110 238 335 211 ...
$ UrbanPop: int 58 48 80 50 91 78 77 72 80 60 ...
$ Rape : num 21.2 44.5 31 19.5 40.6 38.7 11.1 15.8 31.9 25.8 ...
具有每个观察键的数据框第一列没有列header。这样,这个包就很好用了。
我的数据显然有一列header。我应该如何使我的数据看起来像 USArrests 这样才有效,但是我需要将簇号附加回数据?
我的数据有 11 列,包括带有观察名称的第一列,所以我通过使用
跳过第一列来进行聚类[,2:11]
when I use this to visualize using
fviz_cluster(allLfit, data = allLdf[,2:11])
它有效,但情节使用了模棱两可的名字
有什么建议吗??
谢谢!!!
structure(list(PIN = structure(1:5, .Label = c("a", "b", "c",
"d", "e"), class = "factor"), v1 = c(0.8, 0.36, 0.21, 0.84, 0.43
), v2 = c(0.87, 0.01, 0.56, 0.75, 0.98), v3 = c(0.48, 0.13, 0.26,
0.34, 0.83)), row.names = c(NA, 5L), class = "data.frame")
按照 link 中相同的步骤,scale
d 创建数字列 'df',同时将 row.names
设置为第一列,获取 kmeans
('k2') 并在 'k2' 上使用 fviz_cluster
指定 'df' 作为 scale
d 数据集
library(factoextra)
library(cluster)
df <- scale(`row.names<-`(allLdf[-1], allLdf[[1]]))
k2 <- kmeans(df, centers = 2, nstart = 25)
fviz_cluster(k2, data = df)
-输出