title: Create a variable that identifies earliest diagnosis and dates from 3 separate variables in R
body: <p>I have a dataframe in R with multiple columns for different diagnoses and dates associated with each diagnosis.</p>
  <pre><code>df = data.frame(ID = c(1001, 1002, 1003, 1004, 1005),
                  Disease_code_1 = c('I802', 'G200','I802',NA, 'H356'),
                  Disease_code_2 = c('A071',NA,'G20',NA,'H250'),
                  Disease_code_3 = c('H250', NA,NA,NA,NA),
                  Date_of_diagnosis_1 = c('12/06/1997','13/06/1997','14/02/2003',NA,'18/20/2005'),
                  Date_of_diagnosis_2 = c('12/06/1998',NA,'18/09/2001',NA,'12/07/1993'),
                  Date_of_diagnosis_3 = c('17/09/2010',NA,NA,NA,NA))
  
      ID Disease_code_1 Disease_code_2 Disease_code_3 Date_of_diagnosis_1 Date_of_diagnosis_2 Date_of_diagnosis_3
  1 1001           I802           A071           H250          12/06/1997          12/06/1998          17/09/2010
  2 1002           G200           &lt;NA&gt;           &lt;NA&gt;          13/06/1997                &lt;NA&gt;                &lt;NA&gt;
  3 1003           I802            G20           &lt;NA&gt;          14/02/2003          18/09/2001                &lt;NA&gt;
  4 1004           &lt;NA&gt;           &lt;NA&gt;           &lt;NA&gt;                &lt;NA&gt;                &lt;NA&gt;                &lt;NA&gt;
  5 1005           H356           H250           &lt;NA&gt;          18/20/2005          12/07/1993                &lt;NA&gt;
  </code></pre>
  <p>But the dates and first diagnoses are not in order across the variables. I have created a variable to list the date of first diagnosis as it is necessary for my covariates, but also need to know which diagnosis was first. I have tried an ifelse function, but am not sure how to link these together to create a &quot;firstdiagnosis&quot; variable.</p>
  <pre><code>df$mindate &lt;- apply(data[,c(&quot;Disease_code_1&quot;,&quot;Disease_code_2&quot;,&quot;Disease_code_3&quot;)],1,min, na.rm = TRUE)
  </code></pre>