title: In R, use mutate() to create a new column based on conditions by group
body: <p>For each person, there are two types of visits and for each visits, there are date records. The dataset looks like below.</p>
  
  <pre><code>p &lt;-c(1,1,1,2,2,2,2,3,3,3,4)
  type &lt;- c(15,20,20,15,20,15,20,20,15,15,15)
  date &lt;- as.Date.factor(c("2014-02-03","2014-02-04","2014-02-06","2014-01-28","2014-02-03","2014-03-03","2014-03-13","2014-04-03","2014-04-09","2014-12-03","2014-04-05"))
  d &lt;- data.frame(p,type,date)
  </code></pre>
  
  <p>So now the dataset looks like this.</p>
  
  <pre><code>&gt; d
     p type       date
  1  1   15 2014-02-03
  2  1   20 2014-02-04
  3  1   20 2014-02-06
  4  2   15 2014-01-28
  5  2   20 2014-02-03
  6  2   15 2014-03-03
  7  2   20 2014-03-13
  8  3   20 2014-04-03
  9  3   15 2014-04-09
  10 3   15 2014-12-03
  </code></pre>
  
  <p>Now, I'd like to create three new columns. </p>
  
  <ol>
  <li><p>indicating whether a type 20 visit happens in 7 days after the type 15 visit, if yes then the indicator is 1, otherwise 0.(for example, for p2, in the line 4, this value should be 1, and in the line 6, this value should be 0)</p></li>
  <li><p>What is the first date of type 20 visit happened in 7 days after the type 15 visit. If there is no type 20 visit in 7 days after the type 15, then keep it blank. (for example, for p1, the value should be 2014-02-04 instead of 2014-02-06)</p></li>
  <li><p>How many days is between the type 15 visit and type 20 visit happened in 7 days from it. If there is no type 20 visit in 7 days after the type 15, then keep it blank.(for example, the value in line 1 should be 1)</p></li>
  </ol>
  
  <p>I'm a super newbie in R, and basically have no idea of what to do. I tried a for loop within group, but it never works.</p>
  
  <pre><code>group_by(p)%&gt;%
  for(i in i:length(date)){
    *if(type[i]== 15 &amp;&amp; date[i]+7 &gt;= date[i+1:length(date)]){
    indicator = 1
    first_date = 
    days =* #Have no idea how to check in this part
  } else {
    indicator = 0
    first_date = NA
    days = NA
  }
  </code></pre>
  
  <p>The expected output is as below.</p>
  
  <pre><code>   p type       date ind first_date days
  1  1   15 2014-02-03   1 2014-02-04    1 # = 2014-02-04 - 2014-02-03  
  2  1   20 2014-02-04  NA       &lt;NA&gt;   NA
  3  1   20 2014-02-06  NA       &lt;NA&gt;   NA
  4  2   15 2014-01-28   1 2014-02-03    6 # = 2014-02-03 - 2014-01-28   
  5  2   20 2014-02-03  NA       &lt;NA&gt;   NA
  6  2   15 2014-03-03   0       &lt;NA&gt;   NA # since (2014-03-13 - 2014-03-03) &gt; 7   
  7  2   20 2014-03-13  NA       &lt;NA&gt;   NA
  8  3   20 2014-04-03  NA       &lt;NA&gt;   NA #I don't care about the value for type 20 lines
  9  3   15 2014-04-09   0       &lt;NA&gt;   NA
  10 3   15 2014-12-03   0       &lt;NA&gt;   NA
  </code></pre>
  
  <p>So I come up with a new idea. What if we group records by p and type == 15.Then we can use subtraction within groups as days, and the rest will be easy.</p>
  
  <p>I found one way in doing this: </p>
  
  <pre><code> d[,group:= cumsum(type ==15)]
  </code></pre>
  
  <p>However, this will count group when encountering a new type 15 record. How to add p as another grouping condition? </p>