如何获得向量 x 中向量 y 的补码 [英] How can I get the complement of vector y in vector x

查看:24
本文介绍了如何获得向量 x 中向量 y 的补码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

那是 x \ y 使用数学符号.假设

That's x \ y using mathematical notation. Suppose

x <- c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,2,1,1,1,3) 
y <- c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1)

如何获得包含 x 中所有不在 y 中的值的向量.即结果应该是:

How can I get a vector with ALL the values in x that are not in y. i.e the result should be:

2,1,1,3

有一个类似的问题这里.但是,没有一个答案返回我想要的结果.

There is a similar question here. However, none of the answers returns the result that I want.

推荐答案

这里是一个使用 pmatch 的解决方案(这会根据您的需要给出补充"):

Here a solution using pmatch (this gives the "complement" as you require):

x <- c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,2,1,1,1,3)
y <- c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1)
res <- x[is.na(pmatch(x,y))]

来自 pmatch 文档:

如果duplicates.ok 为FALSE,一旦匹配的表的值被排除在搜索后续匹配之外."

"If duplicates.ok is FALSE, values of table once matched are excluded from the search for subsequent matches."

这篇关于如何获得向量 x 中向量 y 的补码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆