Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is one way:</p> <pre><code># create some random data df &lt;- data.frame(y=rnorm(100),x1=rnorm(100), x2=rnorm(100),x3=rnorm(100)) # introduce random NA's df[round(runif(10,1,100)),]$x1 &lt;- NA df[round(runif(10,1,100)),]$x2 &lt;- NA df[round(runif(10,1,100)),]$x3 &lt;- NA # this does the actual work... # assumes data is in columns 2:4, but can be anywhere for (i in 2:4) {df &lt;- df[!is.na(df[,i]),]} </code></pre> <p>And here's another, using <code>sapply(...)</code> and <code>Reduce(...)</code>:</p> <pre><code>xx &lt;- data.frame(!sapply(df[2:4],is.na)) yy &lt;- Reduce("&amp;",xx) zz &lt;- df[yy,] </code></pre> <p>The first statement "applies" the function <code>is.na(...)</code> to columns 2:4 of <code>df</code>, and inverts the result (we want <code>!NA</code>). The second statement applies the logical <code>&amp;</code> operator to the columns of <code>xx</code> in succession. The third statement extracts only rows with <code>yy=T</code>. Clearly this can be combined into one horrifically complicated statement.</p> <pre><code>zz &lt;-df[Reduce("&amp;",data.frame(!sapply(df[2:4],is.na))),] </code></pre> <p>Using <code>sapply(...)</code> and <code>Reduce(...)</code> can be faster if you have very many columns.</p> <p>Finally, most modeling functions have parameters that can be set to deal with NA's directly (without resorting to all this). See, for example the <code>na.action</code> parameter in <a href="http://stat.ethz.ch/R-manual/R-patched/library/stats/html/lm.html" rel="nofollow">lm(...)</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload