title: Handling missing/incomplete data in R--is there function to mask but not remove NAs?
body: <p>As you would expect from a DSL aimed at data analysis, R handles missing/incomplete data very well, for instance:</p>
  
  <p>Many R functions have an <strong><em>na.rm</em></strong> flag that when set to <em>TRUE</em>, remove the NAs:</p>
  
  <pre><code>&gt;&gt;&gt; v = mean( c(5, NA, 6, 12, NA, 87, 9, NA, 43, 67), na.rm=T)
  &gt;&gt;&gt; v
        (5, 6, 12, 87, 9, 43, 67)
  </code></pre>
  
  <p>But if you want to deal with NAs <em>before</em> the function call, you need to do something like this:</p>
  
  <p>to remove each 'NA' from a vector:</p>
  
  <pre><code>vx = vx[!is.na(a)]
  </code></pre>
  
  <p>to remove each 'NA' from a vector and replace it w/ a '0':</p>
  
  <pre><code>ifelse(is.na(vx), 0, vx)
  </code></pre>
  
  <p>to remove entire each row that contains 'NA' from a data frame:</p>
  
  <pre><code>dfx = dfx[complete.cases(dfx),]
  </code></pre>
  
  <p>All of these functions permanently <em>remove</em> 'NA' or rows with an 'NA' in them.</p>
  
  <p>Sometimes this isn't quite what you want though--making an 'NA'-excised copy of the data frame might be necessary for the next step in the workflow but in subsequent steps you often want those rows back (e.g., to calculate a column-wise statistic for a column that has missing rows caused by a prior call to 'complete cases' yet that column has no 'NA' values in it).</p>
  
  <p>to be as clear as possible about what i'm looking for: python/numpy has a class, <em>masked array</em>, with a <em>mask</em> method, which lets you <strong>conceal</strong>--but not remove--NAs during a function call. Is there an analogous function in R?</p>