You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As switching from SAS to R, I really enjoy using data.table for the syntax simplicity and efficiency. But I wonder if a conditional join / merge can be implemented. I found a similar issue in package dplyr. See tidyverse/dplyr#557. Is there anyway to do this in data.table?
Very recently, overlap joins (foverlaps function) has been implemented. It's not yet feature complete, but can do the task you've linked to quite efficiently.
In the next release, we'll try to get it to work more seamlessly. At the moment, we can only join directly, not aggregate while joining (like data.table's x[y, j] syntax). But this is something we'll try to get for the next release.
Now you can calculate sum(data) with by=list(date1adj, date2). But I agree this could be much nicer if we hook this up to [.data.table. Then we'd just have to do:
Currently, I don't think functions in zoo, xts, etc allow to compute a rolling function based on all the observations in, say, the previous year, when the time index is irregular (ie when there is not a row for each period). For sure, they don't allow to do this within each group. Doing .foverlaps with .EACHI would be very useful for these cases!
Would be great to have a minimal reproducible example illustrating the feature that doesn't exist, that could be obtained through foverlaps(). Not familiar with either of those packages or than occasional use of some nice functions.
@ywhuofu Actually after thinking more about this I think one does not need foverlap to do rolling functions. I've written an implementation using findIntervals. The nice thing is that since the function accept vectors, it can also be used within groups. Could you have a look there and tell if that's useful?
@matthieugomez . I am sorry that wasn't following github constantly. I clicked your link and don't see the function you mentioned? Have you found a nicer solution to this problem? Because of this type of joins (sql inequality join), I now use Postgresql as back-ends to do this type of work, and then pips data back to R for analysis.
@arunsrinivasan, I just noticed your post, and I find overlaps() is a great new feature, but lacks some flexibility.
In you example, you have to first add another column that is identical to datestamp as end because foverlaps() requires 2 columns from both data tables.
You also have to modify the start column (minus 1 day) in order to have values that equal to start be included in the join.
It would be much more flexible if one can specify inequality conditions inside the join statement.
Above being said, thank you for the work on this new feature, and look forward to see improvements!
@ywhuofu,
I only wrote a function to roll a given function with respect to a time variable.
For a real inequality join, foverlaps is your best best. It seems @arunsrinivasan plans to incorporate inequality join at some point directly into data.table syntax #830, which would make forverlaps easier to use for time-based joins.
Activity
arunsrinivasan commentedon Sep 26, 2014
Very recently, overlap joins (
foverlapsfunction) has been implemented. It's not yet feature complete, but can do the task you've linked to quite efficiently.In the next release, we'll try to get it to work more seamlessly. At the moment, we can only join directly, not aggregate while joining (like
data.table'sx[y, j]syntax). But this is something we'll try to get for the next release.For the data shown here:
Now you can calculate
sum(data)withby=list(date1adj, date2). But I agree this could be much nicer if we hook this up to[.data.table. Then we'd just have to do:matthieugomez commentedon Oct 29, 2014
Currently, I don't think functions in
zoo,xts, etc allow to compute a rolling function based on all the observations in, say, the previous year, when the time index is irregular (ie when there is not a row for each period). For sure, they don't allow to do this within each group. Doing.foverlapswith.EACHIwould be very useful for these cases!ywhcuhk commentedon Oct 29, 2014
I very much agree on above. Looking forward to have this feature soon. This is the ONLY reason I am still sticking on SAS.
arunsrinivasan commentedon Oct 29, 2014
Would be great to have a minimal reproducible example illustrating the feature that doesn't exist, that could be obtained through
foverlaps(). Not familiar with either of those packages or than occasional use of some nice functions.matthieugomez commentedon Oct 31, 2014
@ywhuofu Actually after thinking more about this I think one does not need foverlap to do rolling functions. I've written an implementation using
findIntervals. The nice thing is that since the function accept vectors, it can also be used within groups. Could you have a look there and tell if that's useful?ywhcuhk commentedon Jun 3, 2015
@matthieugomez . I am sorry that wasn't following github constantly. I clicked your link and don't see the function you mentioned? Have you found a nicer solution to this problem? Because of this type of joins (sql inequality join), I now use Postgresql as back-ends to do this type of work, and then pips data back to R for analysis.
If you have a solution, I am very eager to learn!
Thank you!
ywhcuhk commentedon Jun 3, 2015
@arunsrinivasan, I just noticed your post, and I find
overlaps()is a great new feature, but lacks some flexibility.datestampasendbecausefoverlaps()requires 2 columns from both data tables.startbe included in the join.It would be much more flexible if one can specify inequality conditions inside the join statement.
Above being said, thank you for the work on this new feature, and look forward to see improvements!
matthieugomez commentedon Jun 4, 2015
@ywhuofu,
I only wrote a function to roll a given function with respect to a time variable.
For a real inequality join, foverlaps is your best best. It seems @arunsrinivasan plans to incorporate inequality join at some point directly into data.table syntax #830, which would make forverlaps easier to use for time-based joins.
arunsrinivasan commentedon Mar 9, 2016
This'll be automatically taken care of when #1452 is done.