Difference between unique() and duplicated()

When we work with data, we usually find with an obstacle: repeated values. This type of values don’t represent a critical problem if we have the ability to identify. Once we have that list of repeated values, it is very easy to discard, eliminate or simply extract.

We are going to see two type of functions in R which allow to identify repeated values: unique() and duplicated() function. Besides, as we will see below, we can use these functions with different types of data, such as vectors, matrix or dataframes.

  • As we can see, unique() function uses numeric indicators to determine unique values.
  • Instead, duplicated() function uses logical values to determine duplicated values.

Besides, we can use these functions in matrix:

Now, we will identify unique and duplicated rows, using very common dataframe called iris. Besides, we will also select not repeated rows:

Finally, we can see that we can obtain the same result with iris[unique(iris),] and iris[!duplicated(iris),]