开发者

Bag difference (similar to setdiff() but not for sets)

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 t开发者_运维问答hat the multiplicity is correct, but the general ordered case does seem worth solving.)


There's a sets module that comes close to what you describe. Something like:

library(sets)
gset_difference(as.gset(x), as.gset(y)) 
# gives
{1 [1], 2 [1], 3 [2], 4 [2]}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜