Friday 26 May 2017

outer join data.table R



Just wondering if there is an efficient way to do outer joins with data table such as



a <- data.table(a=c(1,2,3),b=c(3,4,5))
b <- data.table(a=c(1,2),k=c(1,2))
merge(a,b,by="a",all.x=T)



this works fine, but it is not as efficient as the inner join with bigger data, as the following runs very fast, but the above is really slow.



setkey(a,a)
setkey(b,a)
a[b,]

Answer



b[a,] is the "outer join" you're looking for.



Take a look at ?merge.data.table for more specifics.



No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...