首页 > 解决方案 > “x”和“y”长度不同

问题描述

我正在尝试确定最佳集群数量。

# Determine optimal number of clusters
wss<-rep(0,2)
wss[1]<-sum(scale(price[,2:2],scale=FALSE)^2)
for(i in 2:16)
wss[i]<-sum(kmeans(price[,2:2],centers=i)$withinss)
plot(4:2,wss,type="b",xlab="Number of clusters",ylab="Within-cluster sum of squares")

除最后一行外,每一行都有效。最后一个给出错误:

xy.coords(x, y, xlabel, ylabel, log) 中的错误:“x”和“y”长度不同

我已经尝试了其他问题的一些解决方案,但没有运气。有什么建议吗?非常感谢!

样本数据:

    Country        Price

    Albania        1.57
    Andorra        1.24
    Azerbaijan     0.47
    Austria        1.33
    Belarus        0.73
    Belgium        1.54
    Bosnia & Herz. 1.29
    Bulgaria       1.13
    Croatia        1.44
    Czech Rep.     1.32
    Cyprus         1.28
    Denmark        1.74
    Estonia        1.41
    Finland        1.61
    France         1.67
    Georgia        0.9

标签: r

解决方案


wss(y 变量)的长度为 16,但在 x 轴上您使用的是 4:2(长度为 3)。这就是您收到错误的原因。

将 4:2 更改为 17:2 以使 x 和 y 变量的长度相同。喜欢:

plot(17:2,wss,type="b",xlab="Number of clusters",ylab="Within-cluster sum of squares")

推荐阅读