0

I have two lists or similar, and I want to get the items in big_list that are not in little_list.

big_list  <- c(1,2,3,4,5)
little_list  <- c(2,4)
big_list[big_list %in% little_list] # this gives me the interection

But I want the complement (items in big_list that aren't in little_list, i.e. big_list\little_list).

This doesn't work

big_list[big_list ! %in% little_list]

Neither does this

big_list[big_list %in% ! little_list]

I am assuming there is an answer I should have worked out by myself?!

drstevok
  • 715
  • 1
  • 6
  • 15

2 Answers2

3

You may create this function in your source file or Rprofile and load it at start. Pretty convenient.

'%!in%' <- function(x,y)!('%in%'(x,y))

then you could do

big_list[big_list %!in% little_list]
KFB
  • 3,501
  • 3
  • 15
  • 18
2
setdiff(big_list, little_list)
 #[1] 1 3 5

Or

 big_list[!big_list %in% little_list]
 #[1] 1 3 5
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Brilliant, thanks. Took my longer to write the question than it did for you to answer, but quicker than trying to figure it out by myself. Now waiting for SO to let me mark this as answered. – drstevok Oct 10 '14 at 10:20
  • @drstevok I am sure you could have figured it by yourself. – akrun Oct 10 '14 at 10:23