tidyverse / lubridate

Make working with dates in R just that little bit easier
https://lubridate.tidyverse.org
GNU General Public License v3.0
724 stars 207 forks source link

data.table merge doesn't work with intervals #1165

Closed alex-silverman closed 1 week ago

alex-silverman commented 3 months ago

I have the following code to join two data.tables, where one has an interval.

x <- data.table(id = c(1,2), interval = c(lubridate::interval(Sys.Date(), Sys.Date() + 1), NA)) y <- data.table(id = c(1,1,2)) z <- x[y, on = .(id)]

If I run print(z), though, I get the following warning: image

The problem is that z$interval's data has expanded to the 3 rows, but the @start attribute remains at 2.

Perhaps a clue is that the 2nd row, where id == 1, the interval is NA--NA instead of the expected 5/15/2024 -- 5/16/2024, and the 3rd row, where id == 2, is 5/15/2024--NA, instead of the expected NA--NA. It seems like the merge isn't working correctly?

DavisVaughan commented 1 week ago

I think this is just a known general issue where data.table doesn't handle lubridate intervals very well (and S4 in general) https://github.com/Rdatatable/data.table/issues/4315. The solution will have to come from the data.table side unfortunately.