title: Error in apply() : dim(X) must have a positive length
body: <p>The below code gives an error. What causes the error and what can I do to solve it?</p>
  
  <pre><code># Determine number of clusters
  wss &lt;- (nrow(donnees.test$y - esvr1.pred) - 1) * 
         sum(apply(donnees.test$y - esvr1.pred, 2, var))
  for (i in 2:15) wss[i] &lt;- sum(kmeans(donnees.test$y - esvr1.pred,
                                       centers=i)$withinss)
  plot(1:15, wss, type="b", xlab="Number of Clusters",
       ylab="Within groups sum of squares") 
  # K-Means Cluster Analysis
  fit &lt;- kmeans(donnees.test$y - esvr1.pred, 5) # 5 cluster solution
  # get cluster means
  aggregate(donnees.test$y - esvr1.pred, by=list(fit$cluster), FUN=mean)
  # append cluster assignment
  MainData &lt;- data.frame(donnees.test$y - esvr1.pred, fit$cluster) 
  </code></pre>
  
  <p>The error  :</p>
  
  <pre><code>Error in apply(donnees.test$y - esvr1.pred, 2, var) : 
    dim(X) must have a positive length
  </code></pre>