title: How to generate a variable that corresponds to multiple columns?
body: <p>I have a data set for 10 patients that looks like this:</p>
  
  <pre><code>df = data.frame(id=c(1:10),
                 event1=sample(1:4, 10, replace=T),
                 date1=Sys.Date() - sample(100:500, 10, replace=T), 
                 event2=sample(1:4, 10, replace=T),
                 date2=Sys.Date() - sample(100:500, 10, replace=T), 
                 event3=sample(1:4, 10, replace=T),
                 date3=Sys.Date() - sample(100:500, 10, replace=T),
                 follow_up=Sys.Date() - sample(10:100, 10, replace=T)) 
  </code></pre>
  
  <p>Each patient has multiple events, and each event has its corresponding date. An event and date might also be NA if it did not occur. The follow-up date is the last date the patient was checked for events.</p>
  
  <p>I created a variable for outcome bleeding (in this case if an event == 2) as follows:</p>
  
  <pre><code>all_vars_quo &lt;- quo(c(event1, event2, event3))
  
  df %&gt;% rowwise() %&gt;% 
    mutate(bleeding_no = sum(!!all_vars_quo==2, na.rm=TRUE)) %&gt;% 
    mutate(bleeding = if_else(bleeding_no&gt;0,1,0))
  </code></pre>
  
  <p>Now, I would like to add an additional variable "bleeding_date" with the corresponding date of the bleeding. In case more than one bleeding occurred in the same patient, I would like to use the date that is farthest in the past. In case no bleeding occurred, I would like to use the date at follow-up. So far, I was unable to come up with a working piece of code... Any help is much appreciated!</p>