Using the %in% operator in R

Need a quick way to see if a value is contained within a R vector? The %in% operator in R is likely exactly what you’re looking for.

What is the %in% operator in R?

The %in% operator is an operator which services the purposes of an array check function in other languages or the “in” operator in Python. It takes a single value and returns a Boolean True / False result if the value is present within the array you’re checking. Sample code provided below.

# %in% in r example (set up R vector)
> test <- c(1,2,3,4,5,63,2,4,32,47,42)

# %in% in r example (missing value)
> 41 %in% test
[1] FALSE

# %in% in r example (value found)
> 42 %in% test
[1] TRUE

# %in% r - using variables for search value and vector
> secretvalue <- 5
> secretvalue %in% test
[1] TRUE

%in% R applied to checking Data Frames

The same operator can be used to scan a column of an R dataframe to detect if a particular value is present. In the example below, we’re going to check some time stamps in the ChickWeight data set (one of R’s built in data sets, describes how fast chickens grow on different diets). The (fictitious) goal of this analysis is to assess if we have data points for specific days.

# #in# r example - checking a data frame
> head(ChickWeight)
  weight Time Chick Diet
1     42    0     1    1
2     51    2     1    1
3     59    4     1    1
4     64    6     1    1
5     76    8     1    1
6     93   10     1    1

# %in% in r - tests, negative result (no value)
> 5 %in% ChickWeight$Time
[1] FALSE
> 25 %in% ChickWeight$Time
[1] FALSE
> 100 %in% ChickWeight$Time
[1] FALSE

# %in% in r - tests, positive result (no value)
> 6 %in% ChickWeight$Time
[1] TRUE

Using the %in% operator to compare two vectors

You can use the %in% operator to compare two vectors. It will return on array (same number of items as the first vector), identifying the status of each element (True – Present, False – Missing) in the initial vector. This is a handy way to quickly handle complex comparisons where you may have multiple conditions on each side of the logic statement.

# %in% in R example - comparing vectors

# check vector of multiple values - %in% r operator
> c(1, 2) %in% test
[1] TRUE TRUE

# how %in% r operator handles missing values
> c(1, 2, 21) %in% test
[1]  TRUE  TRUE FALSE

Why use the %in% operator?

Like most of the R programming language, there is more than one way to do things here. Checking an array for the presence or absence of a value is a common programming task. That being said, I like the elegant simplicity and readability of using the %in% operator.

It is said that code should be written for humans to read rather than computers to process. With its intuitive elegant simplicity, the %in% operator fulfills this vision.

Want to Learn more R shortcuts?

Check out our tutorial on helpful R functions. In a language where there seems to be several ways to solve any problems, this reference page can help guide you to good options for getting things done. We seek to interject a little Pythonic clarity and sustainability to the “just get it done” world of R programming.

Scroll to top
Privacy Policy