Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

roll data.table with rollends

Tags:

r

data.table

I'm having trouble getting roll=-Inf to work when defining rollends=FALSE. When rollends is not set or set to TRUE, I see the expected result. I would appreciate any suggestions.

library(data.table)

dt1 = data.table(Date=seq(from=as.Date("2013-01-03"),
                          to=as.Date("2013-06-27"), by="1 week"),
                 key="Date")[, ind:=.I]
dt2 = data.table(Date=seq(from=as.Date("2013-01-01"),
                          to=as.Date("2013-06-30"), by="1 day"),
                 key="Date")

I would expect to see 2013-01-05 and 2013-06-26 populated in the output below.

dt1[dt2, roll=-Inf, rollends=FALSE]
           Date ind
  1: 2013-01-01  NA
  2: 2013-01-02  NA
  3: 2013-01-03   1
  4: 2013-01-04   2
  5: 2013-01-05  NA
 ---               
177: 2013-06-26  NA
178: 2013-06-27  26
179: 2013-06-28  NA
180: 2013-06-29  NA
181: 2013-06-30  NA

This is working as expected, the ind data is rolled forward but not outside of the endpoints defined in the dt1 data.table.

dt1[dt2, roll=-Inf]
           Date ind
  1: 2013-01-01   1
  2: 2013-01-02   1
  3: 2013-01-03   1
  4: 2013-01-04   2
  5: 2013-01-05   2
 ---               
177: 2013-06-26  26
178: 2013-06-27  26
179: 2013-06-28  NA
180: 2013-06-29  NA
181: 2013-06-30  NA
like image 382
user338714 Avatar asked Sep 24 '13 14:09

user338714


1 Answers

You're right. This looks like a bug.

UPDATE : Now fixed in v1.8.11 (commit 980). From NEWS :

X[Y,roll=-Inf,rollends=FALSE] didn't roll the middle correctly if Y was keyed. It was ok if Y was unkeyed or rollends left as the default [c(TRUE,FALSE) when roll<0]. Thanks to user338714 for reporting. Tests added.

Thanks for the great question!

like image 84
Matt Dowle Avatar answered Oct 29 '22 05:10

Matt Dowle