title: Conditional statement to give different results elementwise when NA is total or partial
body: <p>I have this data set with 6 variables containing either positive, negative or NA. A new column is required to be generated based on the values of these variables in the following manner:</p>
  
  <p><code>P</code> if ANY of the 6 variables contain a positive result.</p>
  
  <p><code>N</code> if NONE of the 6 variables contain a positive result.</p>
  
  <p><code>NA</code> if ALL of the 6 variables have <code>NA</code>.</p>
  
  <p><strong>MWE</strong></p>
  
  <pre><code>var1 &lt;- c('var1P', 'var1N', 'var1P', 'var1P', NA, 'var1N')
  var2 &lt;- c('var2N', 'var2N', 'var2N', 'var2P', NA, 'var2N')
  var3 &lt;- c('var3N', 'var3N', 'var3N', 'var3P', NA, 'var3N')
  var4 &lt;- c('var4P', 'var4N', 'var4P', 'var4P', NA, 'var4N')
  var5 &lt;- c(NA, 'var5N', 'var5N', 'var5P', NA, 'var5N')
  var6 &lt;- c(NA, NA, 'var6N', 'var6P', NA, 'var6N')
  No &lt;- seq(1:6)
  
  df &lt;- data.frame(No, var1, var2, var3, var4, var5, var6)
  </code></pre>
  
  <p><strong>Code</strong></p>
  
  <pre><code>dfnew &lt;- df %&gt;%
    dplyr::mutate(varif = ifelse(var1 == 'var1P' | var2 == 'var2P' | var3 == 'var3P' | var4 == 'var4P' | var5 == 'var5P' | var6 == 'var6P', 'P', 'N'))
  </code></pre>
  
  <p><strong>Output</strong></p>
  
  <pre><code>  No  var1  var2  var3  var4  var5  var6 varif
     1 var1P var2N var3N var4P  &lt;NA&gt; &lt;NA&gt;      P
     2 var1N var2N var3N var4N var5N  &lt;NA&gt;   &lt;NA&gt;
     3 var1P var2N var3N var4P var5N var6N     P
     4 var1P var2P var3P var4P var5P var6P     P
     5  &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; &lt;NA&gt;        &lt;NA&gt;
     6 var1N var2N var3N var4N var5N var6N     N
  </code></pre>
  
  <p><strong>Question</strong></p>
  
  <p>the <code>varif</code> for No 2 should read <code>N</code> instead of <code>NA</code> to make it different from No 5 where all values were <code>NA</code>.</p>
  
  <p><strong>Failed Trial</strong></p>
  
  <pre><code>impMe &lt;- function (var1, var2, var3, var4, var5, var6) {
    x &lt;-ifelse(is.na(var1) &amp; is.na(var2) &amp; is.na(var3) &amp; is.na(var4) &amp; is.na(var5) &amp; is.na(var6), NA,
               ifelse (var1 == 'var1P' | var2 == 'var2P' | var3 == 'var3P' | var4 == 'var4P' | var5 == 'var5P' | var6 == 'var6P','P','N'))
    return(x)
  }
  
  dfnew &lt;- df %&gt;%
    dplyr::mutate(varif = impMe(var1, var2, var3, var4, var5, var6))
  
    No  var1  var2  var3  var4  var5  var6 varif
     1 var1P var2N var3N var4P  &lt;NA&gt; &lt;NA&gt;      P
     2 var1N var2N var3N var4N var5N  &lt;NA&gt;   &lt;NA&gt;
     3 var1P var2N var3N var4P var5N var6N     P
     4 var1P var2P var3P var4P var5P var6P     P
     5  &lt;NA&gt; &lt;NA&gt;   &lt;NA&gt; &lt;NA&gt;   &lt;NA&gt; &lt;NA&gt;    &lt;NA&gt;
     6 var1N var2N var3N var4N var5N var6N     N
  </code></pre>