+4 votes
in Programming Languages by (4.1k 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

0 votes
by (31.2k 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
[1] FALSE
>

...