To check if a specific item is present in a Set in R, you can use the %in% operator.
In this example,
mySet
containing elements of a specific type.%in%
operator to check if a specific item is present in the vector.%in%
operator returns TRUE
if the item is present in the vector, otherwise it returns FALSE
.# Define vector with items
mySet <- c("apple", "banana", "orange", "grape", "melon")
# Check if item is present
item <- "banana"
isPresent <- item %in% mySet
# Print result
print(paste("Is", item, "present in the Set?", isPresent))
[1] "Is banana present in the Set? TRUE"
In this example,
mySet
containing elements of a specific type.%in%
operator to check if a specific item is not present in the vector.%in%
operator returns FALSE
if the item is not present in the vector.# Define vector with items
mySet <- c("apple", "banana", "orange", "grape", "melon")
# Check if item is not present
item <- "peach"
isNotPresent <- !(item %in% mySet)
# Print result
print(paste("Is", item, "not present in the Set?", isNotPresent))
[1] "Is peach not present in the Set? TRUE"
In this tutorial, we learned How to check if a Specific Item is present in the Set in R language with well detailed examples.