I have a character vector, including some elements that are duplicates e.g.
v <- c("d09", "d11", "d13", "d01", "d02", "d10", "d13")
And another vector that includes single counts of those characters e.g.
x <- c("d10", "d11", "d13")
I want to remove only the first occurrence of each element in x
from the 2nd vector v
. In this example, d13
occurs in x
and twice in v
, but only the first match is removed from v
and the duplicate is kept. Thus, I want to end up with:
"d09", "d01", "d02", "d13"
I've been trying various things e.g. z <- v[!(v %in% x)]
but it keeps removing all instances of the characters in x
, not just the first, so I end up with this instead:
"d09", "d01", "d02"
What can I do to only remove one instance of a duplicated element?
is.element
You can use
match
and negative indexing.produces
match
only returns the location of the first match of a value, which we use to our advantage here.Note that
%in%
andis.element
are degenerate versions ofmatch
. Compare:The last three are all the same, and are basically the coerced to logical version of the first (in fact, see code for
%in%
andis.element
). In doing so you lose key information, which is the location of the first match ofx
inv
and are left only knowing thatx
values exist inv
.The converse,
v %in% x
means something different from what you want, which is "which values inv
are inx
", which won't meet your requirement since all duplicate values will satisfy that condition.