# Compare Two Vectors For Differences in R

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

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 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 –

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

## Author

• Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top