The R %in% operator

R

R Problem Overview


In R, I have am running the following script:

> 1:6 %in% 0:36
[1] TRUE TRUE TRUE TRUE TRUE TRUE

Which is clearly producing a logical vector. I have read the documentation but can't seem to find an operator that would return a scalar based on the result, such that 1:6 %in% 0:36 would simply return TRUE while having 0:37 %in% 0:36 return FALSE.

Does one exist?

R Solutions


Solution 1 - R

You can use all

> all(1:6 %in% 0:36)
[1] TRUE
> all(1:60 %in% 0:36)
[1] FALSE

On a similar note, if you want to check whether any of the elements is TRUE you can use any

> any(1:6 %in% 0:36)
[1] TRUE
> any(1:60 %in% 0:36)
[1] TRUE
> any(50:60 %in% 0:36)
[1] FALSE

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondplanetView Question on Stackoverflow
Solution 1 - RnicoView Answer on Stackoverflow