Created
August 12, 2019 23:16
-
-
Save johndrummond/2ce2bd9f859625a7ecf3689626cdf56c to your computer and use it in GitHub Desktop.
data.table.joins.R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# samples of some joins of data.tables with trivial tables | |
library(data.table) | |
dt1 <- data.table(f1=1:5, f2=sample(letters,5)) | |
dt2 <- data.table(f1=2:6, f3=sample(letters,5)) | |
dt3 <- data.table(f1=c(2,2:6), f3=sample(letters,6)) | |
dt1 | |
dt2 | |
dt3 | |
#inner join | |
merge(dt1, dt2, by="f1") | |
#left join | |
dt1[dt2,on="f1==f1",`:=`(f3=i.f3)] | |
dt1 | |
#left join - note no duplication and new calculated field | |
dt1 <- data.table(f1=1:5, f2=sample(letters,5)) | |
dt1[dt3,on="f1==f1",`:=`(f3=i.f3, f4=paste0(f2,i.f3))] | |
dt1 | |
# outer join | |
merge(dt1, dt2, by="f1", all=TRUE) | |
# right join | |
dt1 <- data.table(f1=1:5, f2=sample(letters,5)) | |
dt2[dt1,on="f1==f1"] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment