小编pan*_*nda的帖子

如果索引列名是连接列名称的前缀,则Data.table与索引连接会产生意外结果

对于两个data.tables的特定设置,连接不会提供我期望的结果.我在我的代码中犯了错误,或者这可能是一个data.table问题?

请看下面的例子.

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
# …
Run Code Online (Sandbox Code Playgroud)

r data.table

7
推荐指数
1
解决办法
200
查看次数

标签 统计

data.table ×1

r ×1