apply(mydata, 2, var) 错误:dim(X) 的长度必须为正

Error in apply(mydata, 2, var) : dim(X) must have a positive length

即使我的数据不是 null.How 我也面临这个错误来克服这个错误。

> dim(som_model$codes)
NULL
> som_model$codes
[[1]]
         Monetray     Frequency     Recency
V1   -0.222667452 -0.3524154924  0.04209164
V2   -0.216275559 -0.3538082762  0.21107959
V3   -0.212684685 -0.3474491246  0.43714767
V4   -0.237204286 -0.3659989235  0.61105297
V5   -0.205093463 -0.3229735212  0.79368476
> mydata <- som_model$codes
> wss <- (nrow(mydata)-1)*sum(apply(mydata,2,var))
Error in apply(mydata, 2, var) : dim(X) must have a positive length
> length(mydata)
[1] 1
> mydata
[[1]]
         Monetray     Frequency     Recency
V1   -0.222667452 -0.3524154924  0.04209164
V2   -0.216275559 -0.3538082762  0.21107959

https://www.r-bloggers.com/self-organising-maps-for-customer-segmentation-using-r/

som_model$codes 看起来像一个列表,在索引 1 处有一个矩阵。您应该可以使用以下方法访问该矩阵:

mydata <- som_model$codes[[1]]

而不是

mydata <- som_model$codes