我在加入2个data.tables时遇到了一个奇怪的行为,其中涉及因子(-key)中的NA:
# unexpeced
A<-data.table(a=factor(c("a",NA)), b=c(4, 5), key="a")
B<-data.table(c=c("a","b","c"), d=c(7, 8, 9), key="c")
A[B, dJoin:=i.d]
a b dJoin
1: NA 5 9
2: a 4 7
但我期待:
#expected
A<-data.table(a=c("a", NA), b=c(4, 5), key="a")
B<-data.table(c=c("a","b","c"), d=c(7, 8, 9), key="c")
A[B, dJoin:=i.d]
a b dJoin
1: NA 5 NA
2: a 4 7