Cartesian Product using data.table package

后端 未结 3 1230
南旧
南旧 2021-02-05 09:51

Using the data.table package in R, I am trying to create a cartesian product of two data.tables using the merge method as one would do in base R.

In base the following w

3条回答
  •  情歌与酒
    2021-02-05 10:16

    If you construct full names from the first and last in the dataframes, you can use CJ (cross-join). You cannot use all three vectors since there would be 99 items.

    > nrow(CJ(dates$date, cust$first.name, cust$last.name ) )
    [1] 99
    

    This returns a data.table object:

    > CJ(dates$date,paste(cust$first.name, cust$last.name) )
                V1           V2
     1: 2012-08-28 George Smith
     2: 2012-08-28  Henry Smith
     3: 2012-08-28     John Doe
     4: 2012-08-29 George Smith
     5: 2012-08-29  Henry Smith
     6: 2012-08-29     John Doe
     7: 2012-08-30 George Smith
     8: 2012-08-30  Henry Smith
     9: 2012-08-30     John Doe
    10: 2012-08-31     John Doe
    11: 2012-08-31 George Smith
    12: 2012-08-31  Henry Smith
    13: 2012-09-01     John Doe
    14: 2012-09-01 George Smith
    15: 2012-09-01  Henry Smith
    16: 2012-09-02 George Smith
    17: 2012-09-02  Henry Smith
    18: 2012-09-02     John Doe
    19: 2012-09-03  Henry Smith
    20: 2012-09-03     John Doe
    21: 2012-09-03 George Smith
    22: 2012-09-04  Henry Smith
    23: 2012-09-04     John Doe
    24: 2012-09-04 George Smith
    25: 2012-09-05 George Smith
    26: 2012-09-05  Henry Smith
    27: 2012-09-05     John Doe
    28: 2012-09-06 George Smith
    29: 2012-09-06  Henry Smith
    30: 2012-09-06     John Doe
    31: 2012-09-07 George Smith
    32: 2012-09-07  Henry Smith
    33: 2012-09-07     John Doe
                V1           V2
    

提交回复
热议问题