R Data.Table: In-memory left join multiple columns from left and right side -
i want merge 2 data tables on multiple columns using data table.
## 2 tables <- data.table(a = 1:4, b = 12:15, c = 10:13) b c 1: 1 12 10 2: 2 13 11 3: 3 14 12 4: 4 15 13 b <- data.table(a = 1:3, b = 12:14, d = 10:12, e = 10:12) b d e 1: 1 12 10 10 2: 2 13 11 11 3: 3 14 12 12
i result:
a <- merge(a, b, = c('a', 'b'), all.x = t) b c d e 1: 1 12 10 10 10 2: 2 13 11 11 11 3: 3 14 12 12 12 4: 4 15 13 na na
this answer showed data table can edit data frame in-memory, it's more efficient when using large data sets. code suggested is
a[b, bb:=i.b, on='a']
how can alter code data frame merged in-memory, , result same above?
if meant join
. here i.d
or i.e
not needed columns unique in 'b'
nm1 <- setdiff(names(b), names(a)) a[b, (nm1) := mget(nm1), on = .(a, b)] # b c d e #1: 1 12 10 10 10 #2: 2 13 11 11 11 #3: 3 14 12 12 12 #4: 4 15 13 na na
Comments
Post a Comment