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

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


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 Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.