I just started using R. I need to plot within cluster variance provided by K-means clustering on a data for 2 through 20 clusters.
Here is my code:
w <- numeric(20)
for (k in 2:20) {
kf <- kmeans(whs2018annexBdatscl,k,nstart=100)
w[k] <- kf$tot.withinss
}
plot(2:20,w,type = "b", lwd= 2, pch= 19, xlab="K", ylab = expression(SS[within]))
I got the error code below: Error in xy.coords(x, y, xlabel, ylabel, log) : 'x' and 'y' lengths differ
When I plot from 1:20, it worked, but I'm supposed to plot 2:20. Please what am I doing wrong.