I have two data tables:
> dt1
--- ---
| V1| V2|
--- ---
| a| a|
| a| b|
| a| c|
| b| c|
| c| d|
--- ---
> dt2
------------------
| id|
------------------
| c(4, 98, 56, 32)|
------------------
dt2
has the following structure:
> str(dt2)
Classes ‘data.table’ and 'data.frame': 1 obs. of 1 variable:
$ id:List of 1
..$ : int 4 98 56 32
- attr(*, ".internal.selfref")=<externalptr>
I am looking for the most efficient way (if possible, based on the data.table
approach) to add column values from the df2
as constant value to each row of the df1
.
Expected result:
--- --- ------------------
| V1| V2| id|
--- --- ------------------
| a| a| c(4, 98, 56, 32)|
| a| b| c(4, 98, 56, 32)|
| a| c| c(4, 98, 56, 32)|
| b| c| c(4, 98, 56, 32)|
| c| d| c(4, 98, 56, 32)|
--- --- ------------------
CodePudding user response:
Do you mean something like this?
dt1[, id:=dt2$id]
Output:
|V1 |V2 |id |
|:--|:--|:-------------|
|a |a |c(4,98,56,32) |
|a |b |c(4,98,56,32) |
|a |c |c(4,98,56,32) |
|b |c |c(4,98,56,32) |
|c |d |c(4,98,56,32) |