'data.table foverlaps with as.yearmon wrong result
I am trying to use foverlaps
from the data.table
package. When I use it together with as.yearmon
I get wrong results. Here is a small example:
library(data.table)
library(zoo)
x<-data.table(date_lower = c("2009-01","2009-10","2010-10"),date_upper = c("2009-01","2009-10","2010-10"),event_id = c("1","2","3"))
y<-data.table(date_lower = c("2009-01","2009-03"),date_upper = c("2009-02","2009-04"))
x[,date_lower:=as.yearmon(date_lower)]
x[,date_upper:=as.yearmon(date_upper)]
y[,date_lower:=as.yearmon(date_lower)]
y[,date_upper:=as.yearmon(date_upper)]
setkey(y,date_lower,date_upper)
foverlaps(x,y,type = "within") #wrong result
x[,date_lower:=as.Date(as.yearmon(date_lower))]
x[,date_upper:=as.Date(as.yearmon(date_upper))]
y[,date_lower:=as.Date(as.yearmon(date_lower))]
y[,date_upper:=as.Date(as.yearmon(date_upper))]
setkey(y,date_lower,date_upper)
foverlaps(x,y,type = "within") #looks fine
Can somebody explain why when I just use as.yearmon
I get wrong matches.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|