To check if a specific item is present in a vector in R, you can use the %in%
operator or the match()
function.
In this example,
num_vec
.%in%
operator to check if the specified item is present in the vector.num_vec <- c(1, 2, 3, 4, 5)
item <- 3
result <- item %in% num_vec
print(result)
[1] TRUE
In this example,
char_vec
.match()
function to check if the specified item is present in the vector.char_vec <- c("apple", "banana", "cherry", "date")
item <- "banana"
result <- match(item, char_vec, nomatch = 0) > 0
print(result)
[1] TRUE
In this tutorial, we learned How to check if a Specific Item is present in the Vector in R language with well detailed examples.