title: Create a Session ID from User ID and time differences
body: <p>I have a similar question to this (<a href="https://stackoverflow.com/questions/21947995/create-a-sessionid-based-on-userid-and-differences-in-timestamp">Create a &quot;sessionID&quot; based on &quot;userID&quot; and differences in &quot;timeStamp&quot;</a>) on creating a 'Session ID'; though my specifications are slightly different.  Perhaps the solution is still apparent in this post but I could not apply it to my needs -- pointing out how the original solution satisfies my question would be equivalent.</p>
  
  <p>My <code>data.table</code> looks like this (dput available below):</p>
  
  <pre><code>unique_visitor_id        datetime            
  100                 2016-07-25 15:43:02      
  100                 2016-08-15 15:35:16      
  101                 2016-08-01 21:24:46      
  101                 2016-08-13 05:32:27      
  101                 2016-08-13 05:33:01      
  101                 2016-08-13 05:33:37      
  101                 2016-08-13 05:34:04      
  101                 2016-08-13 05:37:42      
  101                 2016-08-13 05:38:20      
  102                 2016-09-15 17:28:00      
  102                 2016-09-15 17:31:04      
  103                 2016-07-18 21:19:07 
  </code></pre>
  
  <p><em>NB:</em> <code>datetime</code> was converted to a date object in <code>lubridate</code> via <code>ymd_hms(datetime)</code></p>
  
  <p>What I'd like is a new variable identifying the session, which is a simple integer sequence (does not need to incorporate the visitorID, like the original question) -- a session is defined by visitor, as long as records are &lt;= 30m AND within the same day.  So for example, the first two rows would be two different sessions: though it's the same visitor, the difference in time is >30m.</p>
  
  <p>The desired output from the above data would be:</p>
  
  <pre><code>unique_visitor_id        datetime            session_id
  100                 2016-07-25 15:43:02           1
  100                 2016-08-15 15:35:16           2
  101                 2016-08-01 21:24:46           3
  101                 2016-08-13 05:32:27           4
  101                 2016-08-13 05:33:01           4
  101                 2016-08-13 05:33:37           4
  101                 2016-08-13 05:34:04           4
  101                 2016-08-13 05:37:42           4
  101                 2016-08-13 05:38:20           4
  102                 2016-09-15 17:28:00           5
  102                 2016-09-15 17:31:04           5
  103                 2016-07-18 21:19:07           6
  </code></pre>
  
  <p>If this can be done in a <code>data.table</code> way, that would be desirable.  Again, apologies if I am missing something from the original question's solution!</p>
  
  <p>Here is the <code>dput</code> sample data table:</p>
  
  <pre><code>myDT &lt;- structure(list(unique_visitor_id = c(100L, 100L, 101L, 
                                   101L, 101L, 101L, 101L, 101L, 101L, 102L, 102L, 103L), 
             datetime = structure(c(1469475782, 1471289716, 1470101086, 1471080747, 1471080781, 
                                              1471080817, 1471080844, 1471081062, 1471081100, 1473974880, 
                                              1473975064, 1468891147), 
                                            tzone = "EST5EDT", class = c("POSIXct", "POSIXt"))), 
        .Names = c("unique_visitor_id", "datetime"), 
        sorted = c("unique_visitor_id", "datetime"), 
        class = c("data.table", "data.frame"), 
        row.names = c(NA, -12L))
  </code></pre>