title: How to treat NAs like values when comparing elementwise in R
body: <p>I want to compare two vectors elementwise to check whether an element in a certain position in the first vector is different from the element in the same position in the second vector.<br>
  The point is that I have <code>NA</code> values inside the vectors, and when doing the comparison for these values I get <code>NA</code> instead of <code>TRUE</code> or <code>FALSE</code>.</p>
  
  <p><strong>Reproducible example:</strong>  </p>
  
  <p>Here is what I get:</p>
  
  <pre class="lang-r prettyprint-override"><code>a&lt;-c(1, NA, 2, 2, NA)
  b&lt;-c(1, 1, 1, NA, NA)
  a!=b
  [1] FALSE   TRUE   NA   NA   NA  
  </code></pre>
  
  <p>Here is how I would like the <code>!=</code> operator to work (treat <code>NA</code> values as if they were just another "level" of the variable):<br>
  </p>
  
  <pre><code>a!=b
  [1] FALSE   TRUE   TRUE   TRUE   FALSE
  </code></pre>
  
  <p>There's a possible solution at <a href="http://www.cookbook-r.com/Manipulating_data/Comparing_vectors_or_factors_with_NA/" rel="noreferrer">this link</a>, but the guy is creating a function to perform the task. I was wondering if there's a more elegant way to do that.</p>