For a specific setup of two data.tables, the connection does not deliver the expected results. Am I mistaken in my code, or could it be a data.table problem?
Please see the example below.
library(data.table) # In the code below the join does not deliver the result I would expect DT1 <- data.table(colname=c("test1","test2","test2","test3"), colname_with_suffix=c("other","test","includes test within","other")) DT2 <- data.table(lookup=c("test1","test2","test3"), lookup_result=c(1,2,3)) DT1[colname_with_suffix == "not found", ] # automatically creates index on colname_with_suffix DT1[DT2, lookup_result := i.lookup_result, on=c("colname"="lookup")][] # PLEASE NOTE: same result with slightly different syntax: DT1[DT2, lookup_result := i.lookup_result, on=c(colname="lookup")][] # colname colname_with_suffix lookup_result # 1: test1 other NA # 2: test2 test NA # 3: test2 includes test within NA # 4: test3 other 3 # Expected result: # colname colname_with_suffix lookup_result # 1: test1 other 1 # 2: test2 test 2 # 3: test2 includes test within 2 # 4: test3 other 3
For the following options, the connection works as expected. The unexpected behavior appears to be higher if the index exists in the column with the column name being the prefix of the join column name and both have the same textual content.
# For all following alternatives the join delivers the correct result # (a) Same data tables as above, but no index DT1 <- data.table(colname=c("test1","test2","test2","test3"), colname_with_suffix=c("other","test","includes test within","other")) DT2 <- data.table(lookup=c("test1","test2","test3"), lookup_result=c(1,2,3)) DT1[DT2, lookup_result := i.lookup_result, on=c("colname"="lookup")][] # (b) Index on DT2, but completely different values in indexed column than in join column DT1 <- data.table(colname=c("test1","test2","test2","test3"), colname_with_suffix=c("other","other","other","other")) DT2 <- data.table(lookup=c("test1","test2","test3"), lookup_result=c(1,2,3)) DT1[colname_with_suffix == "not found", ] # automatically creates index on colname_with_suffix DT1[DT2, lookup_result := i.lookup_result, on=c("colname"="lookup")][] # (c) Index on DT2, similar values in indexed column, but indexed column name is not a prefix of join column name DT1 <- data.table(colname=c("test1","test2","test2","test3"), x.colname_with_suffix=c("other","test","includes test within","other")) DT2 <- data.table(lookup=c("test1","test2","test3"), lookup_result=c(1,2,3)) DT1[x.colname_with_suffix == "not found", ] # automatically creates index on x.colname_with_suffix DT1[DT2, lookup_result := i.lookup_result, on=c("colname"="lookup")][]
SessionInfo:
# R version 3.3.2 (2016-10-31)
Note that the same behavior is observed for data.table 1.10.4 and R.Version 3.4.2 under Windows, as well as Ubuntu Linux 14.04.