Home > Back-end >  Data.Table Full Outer Join in R without key
Data.Table Full Outer Join in R without key

Time:02-18

I have a data set as follows: dt1 =

customerid value
1356 0.26
1298 0.38

dt2 =

project_yr value
1 0.31
2 0.79

I want to merge/ join them to get the following output:

customerid value project_yr value
1356 0.26 1 0.31
1356 0.26 2 0.79
1298 0.38 1 0.31
1298 0.38 2 0.79

Is the quickest way to replicate df1 (similar to this Repeat rows of a data.frame) and then merge the second one on?

Or is there a way to do it using the data.table merge function?

Thankyou!

CodePudding user response:

What you are looking for is a cross join

setkey(dt1[,c(k=1,.SD)],k) [ dt2[,c(k=1,.SD)], allow.cartesian=TRUE ][,k:=NULL]

CodePudding user response:

dt1 <- tibble::tribble(
  ~customerid, ~value,
  1356L,   0.26,
  1298L,   0.38
)

dt2 <- tibble::tribble(
  ~project_yr, ~value,
  1L,   0.31,
  2L,   0.79
)

tidyr::expand_grid(dt1, dt2, .name_repair = "minimal")
#> # A tibble: 4 x 4
#>   customerid value project_yr value
#>        <int> <dbl>      <int> <dbl>
#> 1       1356  0.26          1  0.31
#> 2       1356  0.26          2  0.79
#> 3       1298  0.38          1  0.31
#> 4       1298  0.38          2  0.79
  • Related