title: Rolling by group in data.table R
body: <p>I'm trying to roll my function through data.table by group and run into problems. Not sure should I change my function or is my call wrong. Here is simple example:</p>
  
  <p>Data</p>
  
  <pre><code> test &lt;- data.table(return=c(0.1, 0.1, 0.1, 0.1, 0.1, 0.2, 0.2, 0.2, 0.2, 0.2),
                     sec=c("A", "A", "A", "A", "A", "B", "B", "B", "B", "B"))
  </code></pre>
  
  <p>my function</p>
  
  <pre><code>zoo_fun &lt;- function(dt, N) {
    (rollapply(dt$return + 1, N, FUN=prod, fill=NA, align='right') - 1)
  }
  </code></pre>
  
  <p>Running it (I want to create new column momentum, which would be just product of latest 3 observations added by one for each security (so grouping by=sec).</p>
  
  <pre><code>test[, momentum3 := zoo_fun(test, 3), by=sec]
  
      Warning messages:
      1: In `[.data.table`(test, , `:=`(momentum3, zoo_fun(test, 3)), by = sec) :
        RHS 1 is length 10 (greater than the size (5) of group 1). The last 5 element(s) will be discarded.
      2: In `[.data.table`(test, , `:=`(momentum3, zoo_fun(test, 3)), by = sec) :
        RHS 1 is length 10 (greater than the size (5) of group 2). The last 5 element(s) will be discarded.
  </code></pre>
  
  <p>I get that warning and result is not expected:</p>
  
  <pre><code>&gt; test
      return sec momentum3
   1:    0.1   A        NA
   2:    0.1   A        NA
   3:    0.1   A     0.331
   4:    0.1   A     0.331
   5:    0.1   A     0.331
   6:    0.2   B        NA
   7:    0.2   B        NA
   8:    0.2   B     0.331
   9:    0.2   B     0.331
  10:    0.2   B     0.331
  </code></pre>
  
  <p>I was expecting B sec to be filled with 0.728 ((1.2*1.2*1.2) -1) with two NAs in start. What am I doing wrong? Is it that rolling functions won't work with grouping?</p>