title: What does x[is.na(x)] do in R?
body: <p>I'm following the <a href="http://swirlstats.com/" rel="nofollow">swirl</a> tutorial, and one of the parts has a vector x defined as:</p>
  
  <pre><code>&gt; x
   [1]  1.91177824  0.93941777 -0.72325856  0.26998371          NA          NA
   [7] -0.17709161          NA          NA  1.98079386 -1.97167684 -0.32590760
  [13]  0.23359408 -0.19229380          NA          NA  1.21102697          NA
  [19]  0.78323515          NA  0.07512655          NA  0.39457671  0.64705874
  [25]          NA  0.70421548 -0.59875008          NA  1.75842059          NA
  [31]          NA          NA          NA          NA          NA          NA
  [37] -0.74265585          NA -0.57353603          NA
  </code></pre>
  
  <p>Then when we type <code>x[is.na(x)]</code> we get a vector of all <code>NA</code>'s</p>
  
  <pre><code>&gt; x[is.na(x)]
   [1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
  </code></pre>
  
  <p>Why does this happen? My confusion is that <code>is.na(x)</code> itself returns a vector of length 40 with <code>True</code> or <code>False</code> in each entry of the vector depending on whether that entry is <code>NA</code> or not. Why does "wrapping" this vector with x[ ] suddenly subset to the <code>NA</code>'s themselves?</p>