考虑这两个 data.tables,foo
和 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
我知道 foo
和 bar
是一对一的关系。
我想从 bar
中选择行,以便 foo
中的相应行具有不同的值。例如,
f1
和f2
的值在foo
和bar中是相同的,所以排除这个f1
的值已经改变了!将其包含在结果中f1
和f2
的值在foo
和bar中是相同的,所以排除这个f2
的值已经改变!将其包含在结果中bar[c(2,4)]
id f1 f2
1: 2 a b
2: 4 d e
我认为非相等连接在这里会很好用。不幸的是,似乎不支持“不等于”运算符。?
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.
最佳答案
使用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
https://stackoverflow.com/questions/71225879/
相关文章:
c++ - 指针类型运算符 VS const bool 运算符优先级
reactjs - react 路线不工作 | react 路由器
python - 如何修复 python `dlib` 错误 : "symbol not found
c++ - 将字符数组 char u[10] 与字符串文字 "abc"进行比较是否是未定义的行为
php - 如何使用 htaccess 将 URL 中的下划线替换为破折号?
apache-kafka - Spring kafka消费者@retryabletopic无限重试
c++ - std::vector 和动态分配数组有什么区别?
google-cloud-platform - (Terraform) 左操作数 : a numbe