title: r data.table: two questions about 'by' ((1)=.I and (2)='order by')
body: <p>I have two questions about the 'by' in the package data.table.</p>
  
  <p>1) How can one use <code>.I</code> together with it? For example, lets say that we have users who come into a shop at certain times of the day and I want a variable telling me ''the how much-th's time is it that we see this user?''... i.e.</p>
  
  <pre><code>&gt; library(data.table)
  &gt; dt = data.table(visitorId = c(1,2,1,2,1), daytime=c(1,4,7,9,11))
  &gt; dt
         visitorId daytime
  1:         1        1
  2:         2        4
  3:         1        7
  4:         2        9
  5:         1       11
  </code></pre>
  
  <p>the desired solution would produce</p>
  
  <pre><code>       visitorId daytime  howOftenHaveYouBeenHere
  1:         1        1              1
  2:         2        4              1
  3:         1        7              2
  4:         2        9              2
  5:         1       11              3
  </code></pre>
  
  <p>Now I played around with the <code>.I</code> of data.table and nothing gave me what I wanted: The thing .I (:-) sorry, couldnt resist) would have expected to work is <code>dt = dt[, howOftenHaveYouBeenHere := .I, by=visitorId]</code> but this gives</p>
  
  <pre><code>       visitorId daytime  howOftenHaveYouBeenHere
  1:         1        1              1
  2:         2        4              1
  3:         1        7              2
  4:         2        9              2
  5:         1       11              1 &lt;---- not a 3 here!!!
  </code></pre>
  
  <p>I made it work using</p>
  
  <pre><code>dt = dt[, stupid := 1]
  dt = dt[, session := cumsum(stupid), by=visitorId]; print(dt)
  </code></pre>
  
  <p>but it doesnt feel well to do it like this...</p>
  
  <p>2) How to assure that data.table counts sessions by 'time', i.e. until now I did</p>
  
  <p>a) order the table accordingly</p>
  
  <p>b) execute 'by'statements</p>
  
  <p>is that the right way or can one 'smuggle in' SQL's 'ORDER BY' somewhere?</p>
  
  <p>For example: if we reverse the data table from above
  <code>dt = data.table(visitorId = c(1,2,1,2,1), daytime=c(11,9,7,4,1))</code>
  then</p>
  
  <pre><code>dt = dt[, stupid := 1]
  dt = dt[, session := cumsum(stupid), by=visitorId]; print(dt)
  </code></pre>
  
  <p>does not give the desired result. We can fix it by doing it like so:</p>
  
  <pre><code>dt = data.table(visitorId = c(1,2,2,1,1), dayTime=c(11,9,4,7,1))
  dt = dt[order(dayTime, decreasing=FALSE)]
  dt = dt[, stupid := 1]
  dt = dt[, howOftenHaveYouBeenHere := cumsum(stupid), by=visitorId]
  </code></pre>
  
  <p>but is there a 'correct' way of doing it? I.e. is it guaranteed that the order stays fixed while doing the by-statement?</p>
  
  <p>Thanks :-)</p>
  
  <p>FW</p>