title: Create a new column based on a combination of columns + conditions + thresholds in r
body: <p>I have a problem with a piece of code, to start I share a dataset:</p>
  <pre><code>df &lt;- data.frame(PatientID = c(&quot;0002&quot; ,&quot;0002&quot;, &quot;0005&quot;, &quot;0005&quot; ,&quot;0009&quot; ,&quot;0009&quot; ,&quot;0018&quot;, &quot;0018&quot; ,&quot;0039&quot; ,&quot;0039&quot; , &quot;0043&quot; ,&quot;0043&quot;, &quot;0046&quot;, &quot;0046&quot; ,&quot;0048&quot; ,&quot;0048&quot;),
                   sex= c(&quot;F&quot;, &quot;F&quot;, &quot;M&quot;, &quot;M&quot;, &quot;F&quot;, &quot;F&quot;, &quot;M&quot;, &quot;M&quot;,&quot;F&quot;, &quot;F&quot;,  &quot;M&quot;, &quot;M&quot;,  &quot;M&quot;, &quot;M&quot;, &quot;F&quot;, &quot;F&quot;),
                   A1 = c( 1961.810 , 929.466 , 978.166, 1005.820 , 925.752 , 969.469  ,943.398 ,  965.292 , 1996.404 ,  967.047 ,  NA , 893.428 , 921.606 , 976.192 , 929.590 , 950.493),
                   B1 = c(998.988 , NA , 1998.680 , NA , 1020.560 ,  955.540 , 1911.606 , 964.039   ,  988.087 , 1902.367 , 959.338 ,1029.050 , 1987.374 ,1066.400  ,957.512 , 917.597),
                   C1 = c( 1987.140 , 961.810 , 929.466 , 978.166, 969.469 , 943.398  ,936.034,  965.292 , 996.404 , 1920.610 , 967.047, 913.517 , 893.428 , 921.606 , 929.590  ,950.493), 
                   D1 = c( 1961.810 , 929.466 , 978.166, 1005.820 , 925.752 , 969.469  ,1943.398 ,  965.292 , 996.404 ,  967.047 ,  NA , 1893.428 , 921.606 , 976.192 , 929.590 , 950.493),
                   E1 = c(1006.330, 1028.070 ,  954.274 ,1005.910  ,949.969 , 992.820 ,934.407 , 948.913 ,    961.375  ,955.296 , 961.128  ,998.119 ,1009.110 , 994.891 ,1000.170  ,982.763),
                   G1= c(987.140 , 961.810 , 929.466 , 978.166, 969.469 , 943.398  ,936.034,  965.292 , 996.404 , 920.610 , 967.047, 913.517 , 893.428 , 921.606 , 1929.590  ,950.493),
                   A2 = c(NA , 977.146 , NA , 964.315 ,NA , 952.311 , NA , NA , 947.465 , 902.852 ,  NA  ,NA , 930.141 ,1007.790 , NA , 999.414),
                   B2 = c(1998.988 , NA , 1998.680 , NA , NA ,  955.540 , NA , 964.039   ,  988.087 , 1902.367 , NA ,1029.050 , NA ,1066.400  ,NA , 917.597),
                   C2 = c( NA , NA , NA , NA, 969.469 , NA  ,936.034,  965.292 , NA , 1920.610 , 967.047, NA , 1893.428 , 921.606 , 929.590  ,950.493), 
                   D2 = c( 961.810 , NA , 978.166, NA , 925.752 , NA  ,943.398 ,  1965.292 , NA ,  1967.047 ,  NA , 1893.428 , 921.606 , 976.192 , NA , 1950.493),
                   E2 = c(1006.330, 1028.070 ,  NA ,1005.910  ,949.969 , 992.820 ,1934.407 , 948.913 ,    961.375  ,955.296 , NA  ,998.119 ,NA , 994.891 ,1000.170  ,982.763),
                   G2= c(NA , 958.990 , 924.680 , 955.927 , NA , NA  ,973.348 , 984.392 , NA , NA , 995.368 , 1994.997 ,  979.454 , 952.605 ,NA , 956.507), stringsAsFactors = F)
  </code></pre>
  <p>To contextualize the problem: I have 2 sets of metrics defined at visit<code>1</code> (<code>A1,B1,C1,D1,E1,G1</code>) and the same metrics repeated at visit<code>2</code> (<code>A2,B2,C2,D2,E2,G2</code>)
  To diagnose someone at visit 1, I use the following code:</p>
  <pre><code>cols &lt;- 3:8
  df$sex= as.factor(df$sex)
  df %&gt;% mutate(Diagnosis=ifelse(sex == &quot;F&quot; &amp; (rowSums(df[cols] &gt; 1004, na.rm = TRUE) &gt;=3) ,'Yes',
                                        ifelse(sex == &quot;M&quot; &amp; (rowSums(df[cols] &gt; 986, na.rm = TRUE) &gt;=3) ,'Yes','No')))-&gt; df
  </code></pre>
  <p>This piece of code does what I want and it is perfectly fine! :)
  As you can see, I have one threshold for female (1004) and one threshold for male (986). Based on the equation, when the patient has 3 or more metrics above the threshold gets a 'Yes' in the diagnosis.</p>
  <p>Now, the problem comes with visit 2. In this case, the diagnosis has 4 options, the patient can be diagnosed as &quot;ongoing&quot;, &quot;resolved&quot;, &quot;new onset&quot; or &quot;never&quot; disease.</p>
  <p>In theory, the solution should be as easy as apply this piece of code:</p>
  <pre><code>cols &lt;- 9:14
  df$sex= as.factor(df$sex)
  df %&gt;% mutate(Diagnosis=ifelse(sex == &quot;F&quot; &amp; (rowSums(df[cols] &gt; 1004, na.rm = TRUE) &gt;=3) ,'Yes',
                                        ifelse(sex == &quot;M&quot; &amp; (rowSums(df[cols] &gt; 986, na.rm = TRUE) &gt;=3) ,'Yes','No')))-&gt; df
  </code></pre>
  <p>and then a really easy <code>ifelse</code> where were:</p>
  <ul>
  <li>if yes at visit 1 and yes at visit 2 = ongoing</li>
  <li>if yes at visit 1 and no at visit 2 = resolved</li>
  <li>if no at visit 1 and yes at visit 2 = new onset</li>
  <li>if no at visit 1 and no at visit 2 = never</li>
  </ul>
  <p>However the situation is a bit more complex, with a new option called &quot;NPA&quot; (not possible to assess) as there two potential exceptions: as in order to make a reliable judgement, we need to see what happened with those metrics that were elevated. I create a simplified <strong>examples</strong> to ilustrate each of the exceptions:</p>
  <p>A) For example this patient has C1, D1 and E1 elevated at visit 1, however, C2 is NA, therefore this patient at visit 2 would be an NPA</p>
  <pre><code>df &lt;- data.frame(PatientID = c(&quot;112&quot;),
                   sex= c(&quot;F&quot;),
                   A1 = c( 961.810),
                   B1 = c(998.988)
                   C1 = c( 1019.330)
                   D1 = c( 1046.0)
                   E1 = c(1006.330)
                   G1= c(987.140 ),
                   A2 = c(NA )
                   B2 = c(998.988),
                   C2 = c( NA ), 
                   D2 = c( 961.810),
                   E2 = c(1006.330),
                   G2= c(NA), stringsAsFactors = F)
  </code></pre>
  <p>B) In this case C1, D1 and E1 elevated at visit 1, C2 is NA, but A2 is elevated, so regardless to have C1 missing, this patient presents with a clear &quot;yes&quot; at visit 2, that together with a &quot;yes&quot; in visit 1, would be a &quot;ongoing&quot; case.</p>
  <pre><code>df &lt;- data.frame(PatientID = c(&quot;112&quot;),
                   sex= c(&quot;F&quot;),
                   A1 = c( 961.810),
                   B1 = c(998.988)
                   C1 = c( 1019.330)
                   D1 = c( 1046.0)
                   E1 = c(1006.330)
                   G1= c(987.140 ),
                   A2 = c(1800.810)
                   B2 = c(998.988),
                   C2 = c( NA ), 
                   D2 = c( 961.810),
                   E2 = c(1006.330),
                   G2= c(NA), stringsAsFactors = F)
  </code></pre>
  <p>How could I code this. Sorry I know this is a bit rumbling!
  Thanks!</p>
  <p>I attach the solution</p>
  <p><a href="https://i.stack.imgur.com/YedI2.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/YedI2.jpg" alt="enter image description here" /></a></p>