[英]Not Equal To condition in data.table join
Problem问题
I'm trying to join two tables, with several conditions.我试图加入两个表,有几个条件。 I would like to add a Not Equal To condition.
我想添加一个不等于条件。
What I tried我试过的
I tried to use !=
and <> but nothing works, do you know how to do it ?我尝试使用
!=
和 <> 但没有任何效果,你知道怎么做吗?
Example例子
A <- data.table(c("a","a","b","c","d"),c(1,2,3,4,5),c("aa","ab","aa","cd","aa"))
B <- data.table(c("a","a","b","c","d"),c(1,1,5,4,7),c("aa","ab","aa","cd","aa"),c("yes","yes","no","yes","no"))
Jdt <- A[B,on= .(V1,V2,A.V3 != i.V3), `:=`(V4 = i.V4)][is.na(V4), V4 :=0][]
EDIT Desired output编辑所需的输出
V1 V2 V3 V4
1: a 1 aa 0
2: a 2 ab yes
3: b 3 aa no
4: c 4 cd 0
5: d 5 aa no
EDIT 2编辑 2
I'm trying to do something like this:我正在尝试做这样的事情:
A[B,on=c("V1","V3"),`:=`(V42 = i.V4)][V2==i.V2,V4:="0"][,i.V2:=NULL][]
I'd like to rename the columns during the join, bu i get this error:我想在加入期间重命名列,但我收到此错误:
Error in eval(expr, envir, enclos) : object 'i.V2' not found
here's the output i'm looking for这是我正在寻找的输出
V1 V2 V3 V42
1: a 1 aa 0
2: a 2 ab yes
3: b 3 aa no
4: c 4 cd 0
5: d 5 aa no
It seems that you are actually joining on V1
and V3
and checking conditions on V2
.看来您实际上是在
V1
和V3
上加入并检查V2
条件。 Try this:尝试这个:
A[B,on=c("V1","V3")][V2==i.V2,V4:="0"][,i.V2:=NULL][]
# V1 V2 V3 V4
#1: a 1 aa 0
#2: a 2 ab yes
#3: b 3 aa no
#4: c 4 cd 0
#5: d 5 aa no
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.