Vectors are used to store one-dimensional data of the same type in R. In this tutorial, we will look at how to compare two vectors for differences in R. That is, for example, for vectors `vec1`

and `vec2`

, we want the elements of `vec1`

that are not in `vec2`

and vice versa.

## How to compare vectors for differences in R?

You can use the R `setdiff()`

function to compare two vectors for differences in R. Pass the two vectors as arguments to the function.

The following is the syntax –

# unique elements of vec1 not in vec2 setdiff(vec1, vec2)

It returns the elements of the vector `vec1`

that are not in the vector `vec2`

. That is, it essentially calculates the set difference `vec1 - vec2`

. Note that the order of arguments is very important as the set difference `vec1 - vec2`

may not be the same as the set difference `vec2 - vec1`

.

Alternatively, you can also use a combination of the `!`

operator and the `%in%`

operator to select elements of `vec1`

that are not in `vec2`

.

The following is the syntax –

# elements of vec1 not in vec2 vec1[!(vec1 %in% vec2)]

Let’s now look at some examples of using the above methods –

## Vector difference using the `setdiff()`

function

To get elements of the vector `vec1`

that are not in the vector `vec2`

, pass `vec1`

as the first argument and `vec2`

as the second argument to the `setdiff()`

function.

**Data Science Programs By Skill Level**

**Introductory** ⭐

- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science

**Intermediate ⭐⭐⭐**

- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization

**Advanced ⭐⭐⭐⭐⭐**

- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science

**🔎 Find Data Science Programs 👨💻 111,889 already enrolled**

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

Let’s look at an example.

We will create two vectors (with some different elements) and compare them for differences using the `setdiff()`

function.

# create two vector vec1 <- c(1, 2, 2, 3, 4) vec2 <- c(3, 4, 5, 6) # elements of vec1 not in vec2 print(setdiff(vec1, vec2))

Output:

[1] 1 2

We get the elements of `vec1`

that are not in `vec2`

. Also, notice that the returned vector does not contain any duplicates.

You can similarly get the elements of `vec2`

that are not in `vec1`

. For this, pass `vec2`

as the first argument and the `vec1`

as the second argument to the `setdiff()`

function.

# create two vector vec1 <- c(1, 2, 2, 3, 4) vec2 <- c(3, 4, 5, 6) # elements of vec2 not in vec1 print(setdiff(vec2, vec1))

Output:

[1] 5 6

We get the elements of `vec2`

that are not in `vec1`

.

## Vector difference using `!`

and `%in%`

operators

You can also use a combination of the `!`

and the `%in%`

operators to filter the vector `vec1`

such that we get the elements of `vec1`

that are not in `vec2`

.

Let’s look at an example. We will use the same vectors from the examples above.

# create two vector vec1 <- c(1, 2, 2, 3, 4) vec2 <- c(3, 4, 5, 6) # elements of vec1 not in vec2 print(vec1[!(vec1 %in% vec2)])

Output:

[1] 1 2 2

We get elements of `vec1`

that are not in `vec2`

. Notice, that here we also get the duplicate elements.

You can similarly get elements of `vec2`

that are not in `vec1`

.

# create two vector vec1 <- c(1, 2, 2, 3, 4) vec2 <- c(3, 4, 5, 6) # elements of vec2 not in vec1 print(vec2[!(vec2 %in% vec1)])

Output:

[1] 5 6

We get the element of `vec2`

that are not in `vec1`

.

You might also be interested in –

- R – Check If All Elements in a Vector are Equal
- R – Count Distinct Values in a Vector
- Remove Duplicates From a Vector in R

**Subscribe to our newsletter for more informative guides and tutorials. ****We do not spam and you can opt out any time.**