How to check if a vector contains given value in R ?
We can check if a vector contains a given value using the %in% operator. For this, we have to create a vector with some values. And we have to read input from the user for what value to be checked. Or we can assign some value to a variable explicitly. Using the %in% operator with the below-given syntax we can find the element we are looking for.
value_to_be_checked %in% vector
The above operation only returns a boolean value that is true or false.
- Create vector
- Set value to be searched
- Using the given syntax, search for the element in the vector
- If found print true
- Else false
Example 1 :
Example 2 :
Example 3 :