Suppose I have the following data.tables:
dt1 <- data.table(id_1 = 1:4,
name_1 = c("a", "b", "a", "b"),
score_1 = 11:14)
# dt1:
id_1 name_1 score_1
1: 1 a 11
2: 2 b 12
3: 3 a 13
4: 4 b 14
dt2 <- data.table(id_2 = 1:5,
name_2 = rep("a", 5),
score_2 = 21:25)
# dt2:
id_2 name_2 score_2
1: 1 a 21
2: 2 a 22
3: 3 a 23
4: 4 a 24
5: 5 a 25
How can I combine dt1
and dt2
above using the two columns, id_1
and name_1
to get the following data table?
# desired data.table:
id_1 name_1 score_1 score_2
1: 1 a 11 21
2: 2 b 12 NA
3: 3 a 13 23
4: 4 b 14 NA
I have tried the code below, but it does not produce the desired data.table above.
dt3 <- dt1[dt2, on = .(id_1 = id_2, name_1 = name_2)]
Also, can someone point me to a good vignette like the one below that covers the topic of joining / merging?
https://cran.r-project.org/web/packages/data.table/vignettes/datatable-intro.html