# How To Use The unique() Function In R

The unique() function in R can help you detect and remove duplicate elements in your data. Keep reading to get to know how it works.

Table of Contents

## unique() Function In R

This function can take an array, a data frame, or a vector and return it with only unique values. This means unique() will find and eliminate duplicate rows and elements from the data you pass to it.

Its default syntax is as follows (x is the array, data frame, or vector from which you want to remove duplicate values):

unique(x)

### Vectors

Let’s demonstrate the capability of this function with a simple example. Suppose we have a vector like this:

a <- c(1, 2, 3, 3, 4, 5)

In this vector, we have 6 elements, but the third and fourth elements are the same. You can remove one of them with the unique() function:

b <- unique(a)
b

[1] 1 2 3 4 5

Keep in mind that b is also of the same type as the original vector a, but unique() doesn’t copy attributes (such as names) from the input to its returned vector.

In the following example, we name elements in the vector a, but those names don’t get carried by the unique() function to the output vector:

a <- c(1, 2, 3, 2)
names(a) <- c("A", "B", "C", "D")
a

A B C D
1 2 3 2
b <- unique(a)
b

[1] 1 2 3

By default, unique() runs from the first to the last elements in the vector. If there are duplicate elements, it will remove the rightmost elements. If you want to change this behavior, meaning to remove the leftmost identical element, set the fromLast argument to TRUE (it is FALSE by default):

a <- c(1, 2, 3, 2)
unique(a, fromLast = TRUE)

[1] 1 3 2
unique(a)

[1] 1 2 3

You can easily notice the differences in the order of elements when the fromLast argument is enabled and when it is not.

When there are elements you don’t want to remove, even if they are duplicates, put them into the incomparables argument. The unique() function will ignore those elements when scanning your vector:

a

[1] 1 2 3 2
unique(a, incomparables = c(2))

[1] 1 2 3 2

### Matrices

You can also use the unique() function on multi-dimensional data structures in R like matrices.

In this example, we use the matrix() function to create a matrix of 3 columns and 4 rows. As you can see, the first and last rows are identical.

mt <- matrix(rep(1:9, length.out = 12),nrow = 4, ncol = 3, byrow = T)
mt

     [,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
[4,] 1 2 3

We can remove the last row by running the matrix through the unique() function. It will return a 3×3 matrix whose rows are the same as the first three rows of the original matrix.

mt2 <- unique(mt)
mt2

     [,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9

As with vectors, unique() also removes the last identical rows, but change remove the first one instead if you set fromLast to TRUE:

unique(mt, fromLast = TRUE)

     [,1] [,2] [,3]
[1,] 4 5 6
[2,] 7 8 9
[3,] 1 2 3

## Summary

To remove duplicate elements from a vector or other data structures, you can use the unique() function in R. It doesn’t modify your original data. Instead, unique() just returns only unique elements into the output.

Maybe you are interested:

Posted in R