To check if a specific element is present in a list in R, you can use the %in%
operator.
In this example,
my_list
with some elements.%in%
operator to check if the specified element is present in the list.%in%
operator returns TRUE
if the element is present, otherwise FALSE
.my_list <- c(10, 20, 30, 40, 50)
search_element <- 40
is_present <- search_element %in% my_list
print(paste('Element presence in list:', is_present))
Element presence in list: TRUE
In this example,
my_list
with some string elements.%in%
operator to check if the specified string is present in the list.%in%
operator returns TRUE
if the string is present, otherwise FALSE
.my_list <- c('apple', 'banana', 'cherry', 'date', 'elderberry')
search_element <- 'cherry'
is_present <- search_element %in% my_list
print(paste('Element presence in list:', is_present))
Element presence in list: TRUE
In this tutorial, we learned How to check if a Specific Element is present in the List in R language with well detailed examples.