8

Possible Duplicate:
Opposite of %in%

What is the opposite of

matrix[matrix%in%1,]?

!%in% does not work.

I would like to select items that do not contain a certain number.

Community
  • 1
  • 1
user1723765
  • 6,179
  • 18
  • 57
  • 85

2 Answers2

33

If you find yourself using @Joshua's suggestion often, you could easily make your own %notin% operator.

`%notin%` <- Negate(`%in%`)
'a' %notin% c('b', 'c')
# [1] TRUE
Matthew Plourde
  • 43,932
  • 7
  • 96
  • 113
27

You want:

matrix[!matrix %in% 1,]

For clarity's sake, I prefer this, even though the parentheses aren't necessary.

matrix[!(matrix %in% 1),]

Also note that you need to be aware of FAQ 7.31: Why doesn't R think these numbers are equal?.

Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418