Consider these two data.tables, foo and bar.
foo <- data.table(id = c(1,2,3,4), f1 = c("a", "b", "c", "d"), f2 = c("a", "b", "c", "d"))
bar <- data.table(id = c(1,2,3,4), f1 = c("a", "a", "c", "d"), f2 = c("a", "b", "c", "e"))
foo
id f1 f2
1: 1 a a
2: 2 b b
3: 3 c c
4: 4 d d
bar
id f1 f2
1: 1 a a
2: 2 a b
3: 3 c c
4: 4 d e
I know that foo and bar have a 1-1 relationship.
I would like to select rows from bar such that the corresponding row in foo has different values. For example,
f1 and f2 are the same in foo and bar, so exclude this onef1 has changed! include this in the resultf1 and f2 are the same in foo and bar, so exclude this onef2 has changed! include this in the resultbar[c(2,4)]
id f1 f2
1: 2 a b
2: 4 d e
I thought a non-equi join would work great here.. Unfortunately, it seems the "not equals" operator isn't supported.?
foo[!bar, on = c("id=id", "f1!=f1", "f2!=f2")]
# Invalid operators !=,!=. Only allowed operators are ==<=<>=>.
foo[!bar, on = c("id=id", "f1<>f1", "f2<>f2")]
# Found more than one operator in one 'on' statement: f1<>f1. Please specify a single operator.
With data.table:
bar[foo,.SD[i.f1!=x.f1|i.f2!=x.f2],on="id"]
id f1 f2
<num> <char> <char>
1: 2 a b
2: 4 d e
I think this is best (cleanest, but perhaps not fastest?):
bar[!foo, on=.(id,f1,f2)]
id f1 f2
<num> <char> <char>
1: 2 a b
2: 4 d e
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With