[datatable-help] Rolling Join With Groups in One Table

Bernstein, Elliot J EJBernstein at wellington.com
Wed Feb 14 21:18:25 CET 2018


How do I execute a rolling join when one table has groups, and the other does not? For example:

x.dt <- as.data.table(
  expand.grid(date = seq(as.Date("2017-01-01"), as.Date("2017-12-31"), "days"),
              group = c("A", "B")
  )
)
x.dt[, x := rnorm(.N)]
setkey(x.dt, group, date)

y.dt <- data.table(date = seq(as.Date("2017-01-01"), as.Date("2017-12-31"), "months"))
y.dt[, y := month(date)]
setkey(y.dt, date)

result <- y.dt[x.dt, roll = TRUE]

The last line fails because "group" is part of the key in x.dt, but not y.dt:

Error in bmerge(i, x, leftcols, rightcols, io, xo, roll, rollends, nomatch,  :
  typeof x.date (double) != typeof i.group (integer)

I would like the value of y in each row of the result to be the month number (from y.dt), regardless of the value of the group column.

Thanks.

- Elliot

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/datatable-help/attachments/20180214/101da605/attachment.html>


More information about the datatable-help mailing list