Frage

The following code fails

a = data.table(date=seq(ymd('2001-6-30'),ymd('2003-6-30'),by='weeks'))
a = a[,list(date=date,a=rnorm(105),b=rnorm(105))]

b = seq(ymd('2001-6-30'),ymd('2001-07-28'),by='weeks')

a[date %in% b]

with the message

Empty data.table (0 rows) of 3 cols: date,a,b

Can anyone help identify what I'm doing wrong. It should find the data.

War es hilfreich?

Lösung

Nothing to do with lubridate.

Your issue is scoping. You have a column b in your data.table. data.table will look first in the data.table and then up along the search path. It cannot tell you want to look for b in the parent.frame

So, rename your vector in the parent (global) environment

B <- b
a[date %in% B]

         date           a          b
1: 2001-06-30 -1.89904968  0.9230171
2: 2001-07-07  0.08599561 -0.0440927
3: 2001-07-14 -0.28606686  0.4649957
4: 2001-07-21  0.39191680  0.2907855
5: 2001-07-28  0.18732463 -0.1743267
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top