Remove Duplicate Elements from an Object in R Programming - unique() Function
Last Updated :
04 Jun, 2020
Improve
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 NULLExample 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)
[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 10Example 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)
[, 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