'Data.Table Full Outer Join in R without key
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!
Solution 1:[1]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | danlooo |
