title: How to count events over time and group by conditions based on datetimes with Dataframes in R
body: <p>I am being faced with the challenge of building a table by doing some data wrangling with two different datasets.</p>
  <p>DataSet A: Has the information of purchases in a clothing store with the variables: name of client, date of purchase, agent and product purchased during a period of time t.</p>
  <pre><code>df1 &lt;- tibble::tribble(
    ~NAME, ~PRODUCT, ~AGENT,        ~DATE_PURCHASE,
    &quot;Karen&quot;,   &quot;M_14&quot;,  &quot;X_1&quot;, &quot;8-25-20021 18:21:28&quot;,
    &quot;Jean&quot;,   &quot;M_78&quot;,  &quot;X_3&quot;, &quot;8-26-20021 18:11:06&quot;,
    &quot;Jean&quot;,   &quot;M_71&quot;,  &quot;X_4&quot;, &quot;8-26-20021 18:21:01&quot;,
    &quot;Jean&quot;,   &quot;M_64&quot;,  &quot;X_4&quot;, &quot;8-27-20021 20:21:59&quot;,
    &quot;Keith&quot;,   &quot;M_57&quot;,  &quot;X_4&quot;, &quot;8-27-20021 20:21:02&quot;,
    &quot;Alba&quot;,   &quot;M_50&quot;,  &quot;X_1&quot;, &quot;8-28-20021 20:21:03&quot;,
    &quot;Alba&quot;,   &quot;M_43&quot;,  &quot;X_3&quot;, &quot;8-29-20021 20:21:04&quot;,
    &quot;Alex&quot;,   &quot;M_36&quot;,  &quot;X_2&quot;, &quot;8-25-20021 20:21:05&quot;
  )
  </code></pre>
  <p>Dataset B: Has the information of clients who have called the CX SERVICE line of the company during a period of time t and stores the variables name of client, date of call, and type of call.</p>
  <pre><code>df2 &lt;- tibble::tribble(
    ~NAME,        ~TYPE,         ~DATE_OF_CALL,       
    &quot;Karen&quot;,   &quot;COMPLAIN&quot;, &quot;8-26-20021 18:21:28&quot;, 
    &quot;Jean&quot;, &quot;CX_SERVICE&quot;, &quot;8-27-20021 18:11:06&quot;,
    &quot;Jean&quot;,   &quot;COMPLAIN&quot;, &quot;8-28-20021 18:21:01&quot;, 
    &quot;Jean&quot;, &quot;CX_SERVICE&quot;, &quot;8-29-20021 20:21:59&quot;,
    &quot;Keith&quot;, &quot;CX_SERVICE&quot;, &quot;8-29-20021 20:21:02&quot;, 
    &quot;Alba&quot;,   &quot;COMPLAIN&quot;, &quot;8-30-20021 20:21:03&quot;, 
    &quot;Alex&quot;, &quot;CX_SERVICE&quot;, &quot;8-25-20021 21:21:05&quot;, 
  )
  </code></pre>
  <p>I have to build a dataset with the following: I have to create a new variable called &quot;x attempt&quot; that will let me know if this is the first, second, third, etc call of the custumer on the line, and for each custumer bring back on the table the last product bought prior to the very last call received including the type of the call. I know that this may sound confusing so here is an example of the desired table:</p>
  <pre><code>NAME   | x attempt | product | TYPE      | DATE_CALL           | DATE_PURCHASE       |
  Jean|  |     3     |  M_64   |CX_SERVICE | 8-29-20021 20:21:59 | 8-27-20021 20:21:59 |
  </code></pre>
  <p>This result would be correct because on the record... this would be Jean´s third call on the line, the type of the last call was for CX_SERVICE on  8-29-20021 20:21:59 and the last product she bought was a M_64 on 8-27-20021 20:21:59 .</p>