Rolling join grouped by a second variable in data.table

前端 未结 1 1004
终归单人心
终归单人心 2020-12-06 15:53

Hello I would like to perform a rolling join in R using the data.table package. There are multiple matches when joining on the \"Date\" column so I would like t

相关标签:
1条回答
  • 2020-12-06 16:10

    You were almost there.

    You can join on multiple columns simultaneously. So, in addition to "Date", you can include "Field" in the on clause. But please note the description of the roll argument in ?data.table:

    Rolling joins apply to the last join column

    Thus, for "Date" to be used for the rolling join, specify it as the last variable in on:

    library(data.table)
    d1[d2, roll = "nearest", on = .(Field, Date)]
    

    For better verification, the result can be ordered

    d1[d2, roll = "nearest", on = .(Field, Date)][order(Field, Date)]
    
        Field       Date  NlbsAcre      TotN
     1:   12S 2016-05-24        NA 208.62194
     2:   12S 2016-05-27        NA 172.57658
     3:   12S 2016-07-31        NA 318.97092
     4:   12S 2016-08-18        NA 428.54011
     5:   12S 2016-08-29        NA 393.81545
     6:   12S 2017-03-13 44728.184 145.15091
     7:   12S 2017-03-16 44728.184 128.14334
     8:   12S 2017-08-01 12621.083 132.72365
     9:   12S 2017-08-04 12621.083 422.63032
    10:   12S 2017-08-14 12621.083 337.91388
    11:   12S 2017-10-04 22162.203 692.15276
    12:  19-1 2016-05-01 12630.923 476.17492
    13:  19-1 2016-08-15 12630.923 110.70600
    14:  19-1 2016-09-10 12630.923 215.88105
    15:  19-1 2016-09-19 12630.923 224.68906
    16:  19-1 2016-12-16 12630.923 338.59349
    17:  19-1 2017-01-13 12630.923 305.35394
    18:  19-1 2017-03-27 12630.923 435.04925
    19:  19-1 2017-05-30 12630.923 818.80997
    20:     6 2016-05-05        NA 102.53240
    21:     6 2016-06-14        NA 149.06045
    22:     6 2016-06-29        NA 125.82803
    23:     6 2016-06-29        NA 125.82803
    24:     6 2016-07-11        NA  79.24480
    25:     6 2016-07-25        NA  62.24449
    26:     6 2016-08-25        NA  75.77014
    27:     6 2017-01-03  2014.772  47.49660
    28:     6 2017-01-12  2014.772  45.53730
    29:     6 2017-01-17  2014.772  43.92222
    30:     6 2017-02-11  3082.318  21.96791
    31:     6 2017-03-19  2477.083  21.39367
    32:     6 2017-04-17  2427.536  79.03807
    33:     6 2017-07-12        NA 103.52417
    34:     6 2017-07-17        NA  65.53112
    35:     6 2017-09-06        NA  47.40618
    36:     7 2016-06-02        NA 147.49353
    37:     7 2016-07-11        NA  59.26973
    38:     7 2016-08-04        NA  72.62146
    39:     7 2016-08-30        NA  58.27003
    40:     7 2016-08-30        NA  58.27003
    41:     7 2016-10-30        NA  73.88811
    42:     7 2017-02-11  2279.609  21.07551
    43:     7 2017-02-22  2279.609  19.92023
    44:     7 2017-03-19 15842.916  31.71433
    45:     7 2017-05-17        NA  44.96872
    46:     7 2017-07-17        NA  58.53364
    47:   W62 2016-05-05 16764.975  96.72854
    48:   W62 2016-05-31 16764.975  72.96954
    49:   W62 2016-08-31 16764.975  86.33588
    50:   W62 2016-12-05 16764.975  94.19370
    51:   W62 2017-01-02 18874.656 119.39040
    52:   W62 2017-02-22 18874.656  75.46591
        Field       Date  NlbsAcre      TotN
    
    0 讨论(0)
提交回复
热议问题