+5 votes
in Programming Languages by (16.2k 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 (51.1k 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