Open In App

Remove Duplicate Elements from an Object in R Programming – unique() Function

Last Updated : 04 Jun, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

unique() function in R Language is used to remove duplicated elements/rows from a vector, data frame or array.

Syntax: unique(x)

Parameters:
x: vector, data frame, array or NULL

Example 1:




# R program to illustrate
# unique function
   
# Initializing some set of numbers
x <- c(1:10, 5:9)
x
  
# Calling unique() function to print
# unique set of numbers
unique(x)


Output:

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

Example 2:




# R program to illustrate
# unique function
    
# Initializing a matrix
x <- matrix(rep(1:9, length.out = 18), 
            nrow = 6, ncol = 3, byrow = T)
x
   
# Calling unique() function to print
# unique set of numbers in the matrix
unique(x)


Output:

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

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

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads