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.

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.**