fuzzyjoin two data frames using data.table

后端 未结 1 1733
自闭症患者
自闭症患者 2021-01-03 01:57

I have been working on a fuzzyjoin to join 2 data frames together however due to memory issues the join causes cannot allocate memory of…. So I am

1条回答
  •  抹茶落季
    2021-01-03 02:42

    To clarify terminology:

    The data.table approach for your problem does not require a fuzzyjoin with data.table [at least not in the sense of inexact matching]. Instead, you just want to join on data.table columns using non-equal binary operators >=,>, <= and/or <. In data.table terminology those are called "non equi joins".

    Where you titled your question "fuzzyjoin two data frames using data.table" that is just, understandably, after you used library(fuzzyjoin) in your first working attempt. (No problem, just clarifying for readers.)

    Solution using data.table non equi joins to compare date columns:

    You were very close to a working data.table solution where you had:

    dt_final_data <- setDT(df2)[df1, 
                                on = .(ID, date > start_date, date <= end_date)]
    

    To modify it to make it work as you want, simply add a data.table j expression to select the columns you want, in the order you want them EDIT: and prefix the problem column with x. (to tell data.table to return the column from the x side of the dt_x[dt_i,] join) For example, as below calling the column x.date:

    dt_final_data <- setDT(df2)[df1, 
                                .(ID, f_date, ACCNUM, flmNUM, start_date, end_date, x.date, fyear, at, lt), 
                                on = .(ID, date > start_date, date <= end_date)]
    

    This now gives you the output you are after:

    dt_final_data
             ID     f_date               ACCNUM    flmNUM start_date   end_date     x.date fyear         at         lt
     1:   50341 2002-03-08 0001104659-02-000656   2571187 2002-09-07 2003-08-30 2002-12-31  2002 190453.000 173620.000
     2: 1067983 2009-11-25 0001047469-09-010426  91207220 2010-05-27 2011-05-19 2010-12-31  2010 372229.000 209295.000
     3:  804753 2004-05-14 0001193125-04-088404   4805453 2004-11-13 2005-11-05 2004-12-31  2004    982.265    383.614
     4: 1090727 2013-05-22 0000712515-13-000022  13865105 2013-11-21 2014-11-13 2013-12-31  2013  36212.000  29724.000
     5: 1467858 2010-02-26 0001193125-10-043035  10640035 2010-08-28 2011-08-20 2010-12-31  2010 138898.000 101739.000
     6:  858877 2019-01-31 0001166691-19-000005  19556540 2019-08-02 2020-07-24           NA         NA         NA
     7:    2488 2016-02-24 0001193125-16-476010 161452982 2016-08-25 2017-08-17 2016-12-31  2016   3321.000   2905.000
     8: 1478242 2004-03-12 0001193125-04-039482   4664082 2004-09-11 2005-09-03           NA         NA         NA
     9: 1467858 2017-02-16 0001555280-17-000044  17618235 2017-08-18 2018-08-10 2017-12-31  2017 212482.000 176282.000
    10:   14693 2015-10-28 0001193125-15-356351 151180619 2016-04-28 2017-04-20 2016-04-30  2015   4183.000   2621.000
    

    As above, your result for ID=50341 now has date=2002-12-31. In other words, the result column date now comes from df2.date.

    You can of course rename the x.date column in your j expression:

    setDT(df2)[ df1, 
                .(ID, 
                  f_date, 
                  ACCNUM, 
                  flmNUM, 
                  start_date, 
                  end_date, 
                  my_result_date_name = x.date, 
                  fyear, 
                  at, 
                  lt), 
                on = .(ID, date > start_date, date <= end_date)]
    

    Why does data.table (currently) rename columns in non-equi joins and return data from a different column:

    This explanation from @ScottRitchie sums it up quite nicely:

    When performing any join, only one copy of each key column is returned in the result. Currently, the column from i is returned, and labelled with the column name from x, making equi joins consistent with the behaviour of base merge().

    Above makes sense if you keep in mind back before version 1.9.8 data.table didn't have non-equi joins.

    Through and including the current 1.12.2 release of data.table, this (and several overlapping issues) have been the source a lot of discussion on the data.table github issues list. For example: possible inconsistency in non-equi join, returning join columns #3437 and SQL-like column return for non-equi and rolling joins #2706 are just 2 of many.

    However, watch this github issue: Continuing from the above discussions the keen analytical minds of the data.table team are working to make this less confusing in some (hopefully not too distant) future version: Both columns for rolling and non-equi joins #3093

    0 讨论(0)
提交回复
热议问题