Reputation: 24005
In R, is there some easy way to do multi-set (i.e. "bag") differences, similar to setdiff()
, but preserving order and multiplicity in the input vectors?
For example, suppose x <- c(1,2,2,3,1,5,4,4,5,3)
and y <- c(2,1,5,5)
. I'm looking for a function bagdiff()
such that bagdiff(x,y)
is c(2,3,1,4,4,3)
, i.e. the first occurrences of elements of y
in x
have been deleted, with multiplicity.
(In my actual task I won't really care about the order of the output, so it only matters that the multiplicity is correct, but the general ordered case does seem worth solving.)
Upvotes: 4
Views: 816