Conditionally selecting columns in dplyr where certain proportion of values is NA
<h1>Data</h1>

<p>I'm working with a data set resembling the <code>data.frame</code> generated below:</p>

<pre><code>set.seed(1)
dta &lt;- data.frame(observation = 1:20,
                  valueA = runif(n = 20),
                  valueB = runif(n = 20),
                  valueC = runif(n = 20),
                  valueD = runif(n = 20))
dta[2:5,3] &lt;- NA
dta[2:10,4] &lt;- NA
dta[7:20,5] &lt;- NA
</code></pre>

<p>The columns have <code>NA</code> values with the last column having more than 60% of observations <code>NAs</code>.</p>

<pre><code>&gt; sapply(dta, function(x) {table(is.na(x))})
$observation

FALSE 
   20 

$valueA

FALSE 
   20 

$valueB

FALSE  TRUE 
   16     4 

$valueC

FALSE  TRUE 
   11     9 

$valueD

FALSE  TRUE 
    6    14 
</code></pre>

<h1>Problem</h1>

<p>I would like to be able to remove this column in <code>dplyr</code> pipe line somehow passing it to the <code>select</code> argument.</p>

<h2>Attempts</h2>

<p>This can be easily done in <code>base</code>. For example to select columns with less than <em>50%</em> <code>NAs</code> I can do:</p>

<pre><code>dta[, colSums(is.na(dta)) &lt; nrow(dta) / 2]
</code></pre>

<p>which produces:</p>

<pre><code>&gt; head(dta[, colSums(is.na(dta)) &lt; nrow(dta) / 2], 2)
  observation    valueA    valueB    valueC
1           1 0.2655087 0.9347052 0.8209463
2           2 0.3721239        NA        NA
</code></pre>

<hr>

<h2>Task</h2>

<p>I'm interested in achieving the same flexibility in <code>dplyr</code> pipe line:</p>

<pre><code>Vectorize(require)(package = c("dplyr",         # Data manipulation
                               "magrittr"),     # Reverse pipe

char = TRUE)

dta %&lt;&gt;%
  # Some transformations I'm doing on the data
  mutate_each(funs(as.numeric)) %&gt;% 
  # I want my select to take place here
</code></pre>