How can I get the complement of vector y in vector x

This is x \ y using mathematical notation. Let be

 x <- c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,2,1,1,1,3) y <- c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1) 

How can I get a vector with ALL values ​​from x that are not in y. then the result should be:

 2,1,1,3 

There is a similar question here . However, none of the answers returns the result I want.

+4
source share
3 answers

Here's a solution using pmatch (this gives a “complement” as you wish):

 x <- c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,2,1,1,1,3) y <- c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1) res <- x[is.na(pmatch(x,y))] 

From the pmatch documentation:

"If duplicates.ok is FALSE, the values ​​of the table after matching are excluded from the search for subsequent matches."

+8
source

How about this:

 R> x[x!=y] [1] 2 1 1 1 3 Warning message: In x != y : longer object length is not a multiple of shorter object length R> 

This is a difficult problem, I think, as you mix values ​​and positions. A simpler solution relies on one of the “installed” functions in R:

 R> setdiff(x,y) [1] 2 3 

but uses only values, not position.

The problem with the answer I gave you is the implicit use of recirculation and the warning it caused: since your x longer than your y , the first few y values ​​will be reused. But recycling is considered “clean” when the long vector has an integer length length of the long vector. But this is not so, and therefore, I am not sure that we can solve your problem all that is clean.

+4
source

If I understand the problem, you can use table to calculate the difference in the number of elements in each set, and then create a vector based on the difference in these calculations (note that this will not necessarily give you what you gave in your question).

 > diffs <- table(x) - table(factor(y, levels=levels(factor(x)))) > rep(as.numeric(names(diffs)), ifelse(diffs < 0, 0, diffs)) [1] 1 1 2 3 
+3
source

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


All Articles