Skip to content

Support for on-the-fly columns in on #1639

Description

@MichaelChirico

The ability to create columns in on on-the-fly, like we can in j or by, would be appreciated.

As a usage example, this would have simplified my answer to the following self-join / moving-average-type answer:

http://stackoverflow.com/a/36534824/3576984

Reproduced here:

require(data.table)
date <- as.Date(c('2012-01-01','2012-01-01','2012-01-01',
                  '2012-01-02','2012-01-02','2012-01-03',
                  '2012-01-04','2012-01-05','2012-01-05',
                  '2012-01-06','2012-01-06','2012-01-06'))
email <- c('[email protected]', '[email protected]','[email protected]',
           '[email protected]', '[email protected]','[email protected]',
           '[email protected]','[email protected]','[email protected]',
           '[email protected]','[email protected]','[email protected]')
dt <- data.table(date, email)

dt[dt[ , .(date3=date, date2 = date - 2, email)], 
   on = .(date >= date2, date<=date3), 
   allow.cartesian = TRUE
   ][ , .(count = uniqueN(email)), 
      by = .(date = date + 2)]

Would have been prettier / easier to parse as:

dt[dt, on = .(date >= date-2, date<=date), allow.cartesian = TRUE
   ][ , .(count = uniqueN(email)), by = date]

Activity

  1. jangorecki commented on Apr 10, 2016

    @jangorecki
    Member

    looks like a duplicate of #625

  2. aryoda commented on May 4, 2016

    @aryoda

    Big vote for this enhancement since it makes the syntax of data.table shorter, more intuitive and more SQL alike.

  3. arunsrinivasan commented on Jun 25, 2016

    @arunsrinivasan
    Member
  4. added this to the milestone on Jul 5, 2016
  5. Henrik-P commented on Mar 2, 2018

    @Henrik-P
  6. removed this from the milestone on May 10, 2018
  7. franknarf1 commented on Oct 20, 2018

    @franknarf1
    Contributor
  8. jaapwalhout commented on Nov 30, 2018

    @jaapwalhout

    To update: https://stackoverflow.com/q/53559119/2204410
    (or close as a duplicate with one of the links above)

  9. UweBlock commented on Jun 18, 2019

    @UweBlock
    Contributor
  10. ColeMiller1 commented on Feb 22, 2020

    @ColeMiller1
    Contributor

    What would the API be? The difficult part is that users may want to use the newly created on condition. The evaluation would be easier if we could get users to use a helper function if they want to access the new column in j.

    dt[dt, 
       on = .(date >= list(past_date = date-2), 
              date <= date), 
       allow.cartesian = TRUE]
    

    The alternative would be like SQL and have the eval happen twice, once in on and then have users repeat in j

    dt[dt,
       on = .(date >= date-2,
              date<=date), 
       .(x.date, past_date = i.date - 2),
       allow.cartesian = TRUE]
    
  11. UweBlock commented on Mar 8, 2020

    @UweBlock
    Contributor
  12. added
    joinsUse label:"non-equi joins" for rolling, overlapping, and non-equi joins
    on Apr 6, 2020
  13. added
    top requestOne of our most-requested issues
    and removed on Jun 7, 2020
  14. jangorecki commented on Jun 27, 2020

    @jangorecki
    Member

    AFAIR bmerge already do shallow copies of data, so adding new columns there would be probably easiest way to achieve that. We just need a proper handling for is.call of LHS and RHS of each element of on argument using internal .parse_on, and then using that in bmerge. The most tricky part can be handling column names well. Eventually we could allow computed columns to be named, the same as we allow in by argument: .(date2 = date >= date-2, date<=date).

  15. added this to the 1.19.0 milestone on Apr 30, 2026
  16. removed this from the 1.19.0 milestone on May 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

enhancementjoinsUse label:"non-equi joins" for rolling, overlapping, and non-equi joinstop requestOne of our most-requested issues

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions