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 :
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]