How to solve error: "Error in storage.mode(x) <- "double" : 'list' object cannot be coerced to type 'double'" and get results

How to solve error: "Error in storage.mode(x) <- "double" : 'list' object cannot be coerced to type 'double'" and get results

我正在尝试 运行 一些和 k 表示分析。但是我无法解决,因为有一个错误代码。

Error in storage.mode(x) <- "double" : 'list' object cannot be coerced to type 'double'

library(kohonen)

cdata <- read.delim("Cluster.txt", stringsAsFactors=FALSE)

cdata.n <- scale(subset(cdata, select=-c(ID)))

som_model2 <- supersom(data = cdata.n, grid = somgrid(10, 10, "rectangular"))

k = 6

somClusters <- kmeans(som_model2$codes, centers = 6)

建议我使用unlist,结果如下

Cluster means:
        [,1]
1 -0.6702128
2  5.2157179
3  1.2555768
4 -0.2632253
5  2.6067733
6  0.3503127

但是例子的结果如下

## Cluster means:
##   MONEY     VISIT     CROSS       API
## 1 8.2237320 4.8942046 3.6120212 -0.8606384
## 2 -0.2699493 -0.3223770 -0.3357094 -0.2496793
## 3 0.3566740 0.5408914 0.9180064 -0.6252556
## 4 -0.4596952 -0.6586599 -0.9624127 4.2828612
## 5 2.0625665 2.6264913 2.0452184 -0.7848548
## 6 -0.4199132 -0.5746073 -0.7785007 1.1355674

我怎样才能得到这个结果?

我使用这个数据:

https://github.com/woosa7/R_DataAnalytics/blob/08ea98289f4def3c4f72d4c10d3767784b42619b/R_DataMining/data/Cluster.txt

您应该将对象强制转换为矩阵或数据框。 来自 kmeans() 文档:

x = numeric matrix of data, or an object that can be coerced to such a matrix (such as a numeric vector or a data frame with all numeric columns)

somClusters2 <- kmeans(data.frame(som_model2$codes), centers = 6)
somClusters2 
K-means clustering with 6 clusters of sizes 14, 42, 11, 3, 3, 27

Cluster means:
       MONEY      VISIT      CROSS        API
1 -0.4217639 -0.5810061 -0.8014610  1.1764434
2 -0.3080303 -0.3977217 -0.4555428 -0.1649521
3  1.1239411  1.6704112  1.6129638 -0.7312019
4 -0.4606414 -0.6480549 -0.9548480  3.5595079
5  5.1169992  3.9174431  2.7584212 -0.8306366
6  0.1443774  0.2317915  0.5778101 -0.5416495