title: Troubleshooting "undefined columns selected" in R
body: <p>I was trying to remove all columns with a zero variance from my data, using this command</p>
  
  <pre><code>file &lt;- file[,sapply(file, function(v) var(v, na.rm=TRUE)!=0)]
  </code></pre>
  
  <p>This command was working perfectly for my previous datasets, now I am trying to use on a new dataset and it gives me the following error:</p>
  
  <pre><code>Error in `[.data.frame`(file, , sapply(file, function(v) var(v, na.rm = TRUE) !=  : 
  undefined columns selected
  In addition: Warning message:
  In var(v, na.rm = TRUE) : NAs introduced by coercion
  </code></pre>
  
  <p>The problem is I did not select any columns, I just applied the function to all columns! How come I get an error telling me undefined columns selected!
  Any idea what could have gone wrong??</p>
  
  <p>The data looks exactly this way</p>
  
  <pre><code>    col1   col2   col3   col4
  1   FIA    3.5     2.4    NA
  2   DWF    2.1     NA     3.7
  3   LIK    0.25    2.3    1.38
  4   JUW    2.1     4.0    3.2
  </code></pre>