Skip to content
Related Articles

Related Articles

Improve Article

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

  • Last Updated : 04 Jun, 2020

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
My Personal Notes arrow_drop_up
Recommended Articles
Page :