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

时间:2018-01-24 16:53:02

标签: r som

即使mydata不是null,我也面临这个错误。如何克服这个错误。

> 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/

1 个答案:

答案 0 :(得分:0)

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

mydata <- som_model$codes[[1]]

而不是

mydata <- som_model$codes