title: R - summarize and mutate by two columns incl. date
body: <p>I'm trying to summarize a data set based on the time between, within a group. Basically what I have is, like this example, a data set where students buy a course, the courses have levels : 1 till 5, every course is about 2 months and students can just follow the courses from 1 till 5. I want to analyze when students:</p>
  <ul>
  <li>drop out. So that would be, when they have not bought a new course after 2 months</li>
  <li>What the last course was, after they have not bought a
  course in 2 months</li>
  <li>and if they, after they stopped later decided to
  continue again</li>
  </ul>
  <p>my data set looks like this:</p>
  <pre><code>   InvoiceDate StudentName        Course                    
     &lt;date&gt;      &lt;fct&gt;             &lt;fct&gt;                                     
   1 2020-07-26  Tom                Level 1
   2 2019-11-05  Mike               Level 1    
   3 2019-11-05  Elsa               Level 1    
   4 2018-10-15  Mary               Level 1                                      
   5 2020-08-06  Mary               Level 2                                    
   6 2021-10-10  Mary               Level 3   
   7 2021-06-01  Stefan             Level 1       
   8 2021-06-01  Stefan             Party Ticket       
   9 2019-07-04  Julie              Level 1                          
  10 2020-01-02  Julie              Level 2
  </code></pre>
  <p>I would like to end up with a data set like this:</p>
  <pre><code>   FullName  First_Buy         First_Course   Last_Course   Stopped_Date   Continued Course_Continued  Date_Continued      
   1 Tom       2020-07-26        Level 1        Level 1       2020-07-26      No        NA               NA             
   2 Mike      2019-11-05        Level 1        Level 1       2019-11-05      No        NA               NA             
   3 Elsa      2019-11-05        Level 1        Level 1       2019-11-05      No        NA               NA         
   4 Mary      2018-10-15        Level 1        Level 1       2018-10-15      Yes       Level 2          2020-08-06             
   5 Stefan    2021-06-01        Level 1        Level 1       2021-06-01      Yes       Party Ticket     2021-06-01
   6 Julie     2019-07-04        Level 1        Level 1       2019-07-04      Yes       Level 2          2020-01-02  
  </code></pre>
  <p>I do have a start functions, but it gives me errors, which I don't understand..</p>
  <p>What I have now is:</p>
  <pre><code>new_df &lt;-df %&gt;% 
    group_by(FullName) %&gt;% 
    mutate(InvoiceDate = dmy(InvoiceDate),
           Diff_Months = abs((interval(InvoiceDate, lag(InvoiceDate, 
                                                        default = first(InvoiceDate))) %/% 
                                months(1)))) %&gt;% 
    summarise(First_Buy = first(InvoiceDate),
              First_Course = first(InvoiceLineDescription),
              Last_Course = last(InvoiceLineDescription[Diff_Months &lt;= 2]),
              Stopped_Date = InvoiceDate[InvoiceLineDescription == Last_Course],
              Continued = ifelse(Last_Course == last(InvoiceLineDescription), &quot;No&quot;, &quot;Yes&quot;),
              Course_Continued = ifelse(Continued == &quot;Yes&quot;, last(InvoiceLineDescription), NA))
  </code></pre>
  <p>But this gives me the following warning messages for each name:</p>
  <pre><code>Warning messages:
  1: Problem with `mutate()` column `InvoiceDate`.
  i `InvoiceDate = dmy(InvoiceDate)`.
  i All formats failed to parse. No formats found.
  i The warning occurred in group 1: FullName = Tom.
  </code></pre>
  <p>And it gives me the following new_df:</p>
  <pre><code>   FullName          First_Buy First_Course    Last_Course Stopped_Date Continued Course_Continued
     &lt;fct&gt;             &lt;date&gt;    &lt;fct&gt;           fct&gt;       &lt;date&gt;       &lt;lgl&gt;     &lt;lgl&gt;           
   1 Tom               NA        Level 1         NA          NA           NA        NA              
   2 Mike              NA        Level 1         NA          NA           NA        NA              
   3 Elsa              NA        Level 1         NA          NA           NA        NA              
   4 Mary              NA        Level 1         NA          NA           NA        NA              
   5 Mary              NA        Level 1         NA          NA           NA        NA              
   6 Mary              NA        Level 1         NA          NA           NA        NA              
   7 Stefan            NA        Level 1         NA          NA           NA        NA              
   8 Stefan            NA        Level 1         NA          NA           NA        NA              
   9 Julie             NA        Level 1         NA          NA           NA        NA              
  10 Julie             NA        Level 1         NA          NA           NA        NA  
  </code></pre>
  <p>It looks like it is not getting my date right, so I tried to change the format or type of that column, but so far nothing has worked. Somebody know how I can get to what I want :)?</p>