Remove first occurrence of elements in a vector from another vector

拥有回忆 提交于 2019-12-03 10:44:38

You can use match and negative indexing.

v[-match(x, v)]

produces

[1] "d09" "d01" "d02" "d13"

match only returns the location of the first match of a value, which we use to our advantage here.

Note that %in% and is.element are degenerate versions of match. Compare:

match(x, v)            # [1] 6 2 3
match(x, v) > 0        # [1] TRUE TRUE TRUE
x %in% v               # [1] TRUE TRUE TRUE
is.element(x, v)       # [1] TRUE TRUE TRUE

The last three are all the same, and are basically the coerced to logical version of the first (in fact, see code for %in% and is.element). In doing so you lose key information, which is the location of the first match of x in v and are left only knowing that x values exist in v.

The converse, v %in% x means something different from what you want, which is "which values in v are in x", which won't meet your requirement since all duplicate values will satisfy that condition.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!