2

This is likely a repeat question but I did some searching and maybe couldn't find the right keywords to search by...

Is there a function which tests if a variable value is contained in a particular set of values?

For example, assume I had this magically function 'contains', I would like it to have the following property:

contains(value = 5, set = c(2:6)) would return TRUE, where

contains(value = 5, set = c(2,3,4,6,7)) would return FALSE.

Obviously, I could create this function, but I'm hoping a package or option exists.

amonk
  • 1,769
  • 2
  • 18
  • 27
jameselmore
  • 442
  • 9
  • 20

1 Answers1

6

There are several very simple ways to do this. The %in%-Operator is propably the most intuitive.

> 5 %in% 2:6
[1] TRUE

See ?matchfor some more information.

ChrKoenig
  • 901
  • 1
  • 9
  • 23