What I'm trying to achieve is similar to SQL
select *
from T left join
T2 on T1.key2 = T2.key2
left join T3 on T1.key3 = T3.key3
But in R using data.table
. Specifically, 'append' two columns from T2 and one column from T3 to the first table.
My current solution looks like
setkey(t1,key2)
t1 <- t2[t1]
setkey(t1,key3)
t1 <- t3[t1]
setkey(t1,key1) # restore the original key
This copies the entire t1 twice and is not too quick. On the 'inside' t1 is a quite large dataset ~ 1mln rows and t2 and t3 are just small 'mapping' tables.
I think using J(..)
could help, but I'm new to data.tables and can't really understand how to apply it here.
Could somebody please suggest a faster solution for the join? Thanks