The %in% operator is a wrapper for the match function returning "a vector of the same length as x". For instance:
> match(c("a", "b", "c"), c("a", "a"), nomatch = 0) > 0
## [1] TRUE FALSE FALSE
When used within i of data.table, however
(dt1 <- data.table(v1 = c("a", "b", "c"), v2 = "dt1"))
v1 v2
1: a dt1
2: b dt1
3: c dt1
(dt2 <- data.table(v1 = c("a", "a"), v2 = "dt2"))
v1 v2
1: a dt2
2: a dt2
dt1[v1 %in% dt2$v1]
v1 v2
1: a dt1
2: a dt1
duplicates are obtained. Should the expected behaviour of %in% within i of data.table not give the same result as
dt1[dt1$v1 %in% dt2$v1]
v1 v2
1: a dt1
i.e. without duplicates?
data.table_1.9.5- akrundata.tablehas a special method for character matching called%chin%, so you could aswell trydt1[v1 %chin% dt2$v1]- David Arenburgdata.table_1.9.4withR version 3.1.2 (2014-10-31)onPlatform: x86_64-w64-mingw32/x64 (64-bit)- Jensdt1[v1 %chin% dt2$v1]does indeed NOT give duplicates. - Jenslibrary(devtools); install_github("Rdatatable/data.table", build_vignettes = FALSE)- David Arenburg