Finding elements that do not overlap between two vectors

r find common elements in two vectors
r intersect multiple vectors
r difference between two vectors
opposite of intersect in r
find overlap elements in r
find elements not in list r
intersect r
r setdiff

I'm trying to identify elements which are not included in the other vector. For instance in two vectors I have

list.a <- c("James", "Mary", "Jack", "Sonia", "Michelle", "Vincent")

list.b <- c("James", "Sonia", "Vincent")

is there a way to verify which people do not overlap? In the example, I would want to get the vector result that contains Mary, Jack, and Michelle.

Any suggestions will help!

Yes, there is a way:

setdiff(list.a, list.b)
# [1] "Mary"     "Jack"     "Michelle"

outersect(): The opposite of R's intersect() function, The closest I found was setdiff() but the order of the input vectors your content on R-bloggers? click here if you have a blog, or here if you don't. To find the non-duplicated elements between two or more vectors (i.e. the  finding overlapping values in multiple vectors. Learn more about overlapping

I think it should be mentioned that the accpeted answer is is only partially correct. The command setdiff(list.a, list.b) finds the non-overlapping elements only if these elements are contained in the object that is used as the first argument!.

If you are not aware of this behaviour and did setdiff(list.b, list.a) instead, the results would be character(0) in this case which would lead you to conclude that there are no non-overlapping elements.

Using a slightly extended example for illustration, an obvious quick fix is:

list.a <- c("James", "Mary", "Jack", "Sonia", "Michelle", "Vincent")
list.b <- c("James", "Sonia", "Vincent", "Iris")

c(setdiff(list.b, list.a), setdiff(list.a, list.b))
# [1] "Iris"     "Mary"     "Jack"     "Michelle" 

Sets: union , intersect and setdiff - The R Book [Book], Sets: union, intersect and setdiff There are three essential functions for The union of two sets is everything in the two sets taken together, but counting elements only It is the material that is in the first named set, that is not in the second named set. The result is a logical vector whose length matches the vector on the left. A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

An extended answer based on the comments from Hadley and myself: here's how to allow for duplicates.

Final Edit: I do not recommend anyone use this, because the result may not be what you expect. If there is a repeated value in x which is not in y, you will see that value repeated in the output. But: if, say, there are four 9s in x and one 9 in y, all the 9s will be removed. One might expect to retain three of them; that takes messier code.

mysetdiff<-function (x, y, multiple=FALSE) 
{
    x <- as.vector(x)
    y <- as.vector(y)
    if (length(x) || length(y)) {
        if (!multiple) {
             unique( x[match(x, y, 0L) == 0L])  
              }else  x[match(x, y, 0L) == 0L] 
        } else x
}

Rgames> x
[1]  8  9  6 10  9
Rgames> y
[1] 5 3 8 8 1
Rgames> setdiff(x,y)
[1]  9  6 10
Rgames> mysetdiff(x,y)
[1]  9  6 10
Rgames> mysetdiff(x,y,mult=T)
[1]  9  6 10  9
Rgames> mysetdiff(y,x,mult=T)
[1] 5 3 1
Rgames> setdiff(y,x)
[1] 5 3 1

How to get common values between two multi sets?, But How do I find the common values ? I don't think I can use intersect() method as it expects vectors with no duplicate values. I have many many repetitions. Note​:  Finding which element of a vector is between two values in R. I have two vectors x and y. I would like to find which Finding elements that do not overlap

A nice one-liner that applies to duplicates:

anti_join(data_frame(c(1,1,2,2)), data_frame(c(1,1)))

This returns the data frame {2,2}. This however doesn't apply to the case of 1,2 in 1,1,2,2, because it finds it twice

Set Operations, Usage. union(x, y) intersect(x, y) setdiff(x, y) setequal(x, y) is.element(el, set) vectors (of the same mode) containing a sequence of items (conceptually) with no  @abroekhof overlap is the formula that obtain overlap between 2 category in library. in fact my matrix have 200 rows and 650 column. so I need to save the result. because when i use over = d/a; not save in another matrix that include 500 rows and 500 column. i need save result. – sima412 Aug 22 '13 at 15:12

Set intersection of two arrays - MATLAB intersect, The 'rows' option does not support cell arrays, unless one of the inputs is either a Find the values common to both A and B , as well as the index vectors ia and ib , such Create a 2-by-3 matrix containing elements of numeric type double . Unique values that exist in either vectors. The third is tricky because you do not have vectors of equal lengths. Hence there is no index that could be mapped. So the option in that case should be the intersection of the unique values from each vector as @michaelg mentioned. Here are a few ways depending on what you want to do:

outersect(): The opposite of R's intersect() function, The closest I found was setdiff() but the order of the input vectors produces different To find the non-duplicated elements between two or more vectors (i.e. the 'yellow sections of the diagram above) Not quite what I'm after. The following describes the shape of C when the inputs are vectors or matrices and when the 'legacy' flag is not specified: If the 'rows' flag is not specified, then C is a column vector unless both A and B are row vectors, in which case C is a row vector.

Set Operations for Character Strings · UC Business Analytics R , To obtain the elements of the union between two character vectors use union() : To obtain the non-common elements, or the difference, of two character vectors use setdiff() : # returns elements in set_1 not in set_2 setdiff(set_1, set_2) ## [1] "​dogfish" "summit" "odell" Identifying if Elements are Contained in a String. Hot to find the matched values between two vectors and also related indeces. Follow 257 views (last 30 days) Juster on 17 Jun 2012. Vote. 0 ⋮ Vote. 0.

Comments
  • Just be aware that all the functions in this group (setdiff, intersect, union, etc) will ignore duplicates. If you have lists with duplicate values, you'll have to play around a bit. In fact there was a SO question, well answered, for just this problem a couple days ago. -- which of course now I can't find :-(
  • @CarlWitthoft if you look at the source for setdiff you'll see that it's easy to modify to not ignore duplicates
  • @hadley I posted a 'flexible' version of setdiff, just FYI :-)
  • I would store a temporary to the result of the if statement, and call unique on that in the case !multiple. It would be easier to read.