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

Is there any simple way in R to make multi-level (ie, “bagged”) differences similar to setdiff() , but preserving the 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 am looking for a function bagdiff() such that bagdiff(x,y) is c(2,3,1,4,4,3) , that is, the first occurrences of the elements y in x were removed with multiplicity.

(In my real problem, I will not care about the output order, so it is only important that the multiplicity is correct, but the general ordered case is really worth solving.)

+4
source share
1 answer

There is a sets module that is close to what you are describing. Sort of:

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

Source: https://habr.com/ru/post/1342344/


All Articles