title: PCA and Constant-Zero Column Error
body: <p>I have a question about PCA using the caret package and an error message I'm getting, "cannot rescale a constant/zero column to unit variance".</p>
  
  <p>Consider two sets of similar code.  The first works just fine:</p>
  
  <pre><code>a = c(0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, -1, -1, NA)
  b = c(1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, -1, -1, NA)
  c = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  1,  0,  0)
  
  df = data.frame(a, b, c)
  
  trans = preProcess(df, method = c("center", "scale", "pca"))
  </code></pre>
  
  <p>The variance of each column can be seen as:</p>
  
  <pre><code>apply(df, 2, var, na.rm=TRUE)
  </code></pre>
  
  <p>Note that the variance of column "c" is 0.11</p>
  
  <p>Let's say I change the <strong><em>second to last</em></strong> integer in column "c" to 1 instead of 0, and then run the same code:</p>
  
  <pre><code>a = c(0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, -1, -1, NA)
  b = c(1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, -1, -1, NA)
  c = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  1,  1,  0)
  
  df = data.frame(a, b, c)
  
  trans = preProcess(df, method = c("center", "scale", "pca"))
  </code></pre>
  
  <p>I get an error message:</p>
  
  <pre><code>Error in prcomp.default(x, scale = TRUE, retx = FALSE) : 
    cannot rescale a constant/zero column to unit variance
  </code></pre>
  
  <p>If you look at the variance for column c, it's 0.059:</p>
  
  <pre><code>apply(df, 2, var, na.rm=TRUE)
  </code></pre>
  
  <p>Can anyone please help me understand the difference between these two sets of code and why the second gives an error when the first does not?</p>
  
  <p>Thank you</p>