+5 votes
in Programming Languages by (37.5k points)
How can I find out if a specified element is present in an R list?

I tried the "in" operator, but it returned an error.

1 Answer

+1 vote
by (233k points)

You need to use the "%in%" operator instead of the "in" operator. If the specified item is present in the list, it will return TRUE, otherwise, FALSE.

Here is an example:

> a <- list("apple", "banana", "tea", "cow", "lotus", "tiger", "mango")
> "tea" %in% a
[1] TRUE
> "coffee" %in% a

Related questions