title: dplyr mutuate add new column value only once a different column == specific value
body: <p>I have a dataframe with the following structure (summary example, not actual)</p>
  
  <pre><code>dput(df1)
  structure(list(MedID = c(111, 111, 111, 111, 111, 111, 222, 222, 
  222, 222, 222), Service = structure(c(1L, 1L, 2L, 1L, 1L, 3L, 
  3L, 2L, 1L, 1L, 3L), .Label = c("Acute care", "Ext care", "Outpt 
  care"), class = "factor"), AdmitDate = structure(c(16832, 16861, 
  16892, 16922, 16953, 16983, 17181, 17212, 17240, 17271, 17301), class 
  = "Date"), Flag = c(0, 0, 99, 0, 0, 0, 0, 99, 0, 0, 0)), .Names = 
  c("MedID", "Service", "AdmitDate", "Flag"), row.names = c(NA, -11L), 
  class = "data.frame")
  &gt; df1
      MedID  Service   AdmitDate  Flag
  1    111 Acute care 2016-02-01    0
  2    111 Acute care 2016-03-01    0
  3    111   Ext care 2016-04-01   99
  4    111 Acute care 2016-05-01    0
  5    111 Acute care 2016-06-01    0
  6    111 Outpt care 2016-07-01    0
  7    222 Outpt care 2017-01-15    0
  8    222   Ext care 2017-02-15   99
  9    222 Acute care 2017-03-15    0
  10   222 Acute care 2017-04-15    0
  11   222 Outpt care 2017-05-15    0
  </code></pre>
  
  <p>I wish to use dplyr, group_by(MedID) and mutate to add a column in a new dataframe (let's call it Flag2 in df2) such that within each patient (MedID) the df2$Flag2 column == 1 <em>for every subsequent row within that unique MedID</em> but only after the df1$Flag column == 99, otherwise the df2$Flag2 column gets a 0. I can code this as desired if df1$Flag == 99 in the first row of a MedID, but otherwise my code either produces 1 in df2$Flag2 only in the row where df1$Flag == 99, <em>or</em> it produces a 1 for all rows in a given MedID where df1$Flag == 99. The desired output is:</p>
  
  <pre><code>dput(df2)
  structure(list(MedID = c(111, 111, 111, 111, 111, 111, 222, 222, 
  222, 222, 222), Service = structure(c(1L, 1L, 2L, 1L, 1L, 3L, 
  3L, 2L, 1L, 1L, 3L), .Label = c("Acute care", "Ext care", "Outpt 
  care"), class = "factor"), AdmitDate = structure(c(16832, 16861, 
  16892,16922, 16953, 16983, 17181, 17212, 17240, 17271, 17301), class 
  = "Date"),Flag = c(0, 0, 99, 0, 0, 0, 0, 99, 0, 0, 0), Flag2 = c(0, 
  0, 1, 1, 1, 1, 0, 1, 1, 1, 1)), .Names = c("MedID", "Service", 
  "AdmitDate", "Flag", "Flag2"), row.names = c(NA, -11L), class = 
  "data.frame")
  &gt; df2
      MedID    Service  AdmitDate Flag Flag2
  1    111 Acute care 2016-02-01    0     0
  2    111 Acute care 2016-03-01    0     0
  3    111   Ext care 2016-04-01   99     1
  4    111 Acute care 2016-05-01    0     1
  5    111 Acute care 2016-06-01    0     1
  6    111 Outpt care 2016-07-01    0     1
  7    222 Outpt care 2017-01-15    0     0
  8    222   Ext care 2017-02-15   99     1
  9    222 Acute care 2017-03-15    0     1
  10   222 Acute care 2017-04-15    0     1
  11   222 Outpt care 2017-05-15    0     1
  </code></pre>
  
  <p>Here is a snipit example of the code, but not complete since it does not execute properly... Do I need to nest the mutate within a For loop, that seems like mingled R coding? :( Note: df1$Flag can only == 99 once per MedID, which I think should make it easier.</p>
  
  <pre><code>`df2 &lt;- df1 %&gt;% `
      `group_by(MedID) %&gt;%`
      `mutate(Flag2 = ifelse(df1$Flag == 99, 1, 0))`  
  </code></pre>