What are Unique functions in R?

Unique() function in R Programming Language it is used to return a vector, data frame, or array without any duplicate elements/rows.

Syntax: unique(x, incomparables, fromLast, nmax, …,MARGIN) 

Parameters: This function accepts some parameters which are illustrated below: 

x: This parameter is a vector or a data frame or an array or NULL. 

incomparables: This parameter is a vector of values that cannot be compared. If its value is FALSE, that means that all values can be compared, and maybe the only value accepted for methods other than the default. It will be coerced internally to the same type as x.

fromLast: This parameter indicates that if duplication should be considered from the last, i.e., the rightmost of identical elements will be kept. Its value is logical i.e., either true or false. 

nmax: This parameter says the maximum number of unique items expected. …: This is the arguments for particular methods. 

MARGIN: This parameter says the array margin to be held fixed. Return value: This function returns a vector, data frame, or array without any duplicate elements/rows.

Example:


# R program to illustrate # unique() function 

# Initializing an input vector with some 
# duplicate values 
A <- c(1, 2, 3, 3, 2, 5, 6, 7, 6, 5) 

# Calling the unique() function over the 
# above vector to remove duplicate values 
unique(A)

Output:

[1] 1 2 3 5 6 7