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:
# R program to illustrate# unique function # Initializing some set of numbersx <- c(1:10, 5:9)x # Calling unique() function to print# unique set of numbersunique(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 matrixx <- 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 matrixunique(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


