Unique() Function in R - GeeksforGeeks

30 Sep, 2021
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.
The below example shows the process to return the unique elements from the specified vector.
R
# R program to illustrate# unique() function # Initializing an input vector with some# duplicate valuesA <- c(1, 2, 3, 3, 2, 5, 6, 7, 6, 5)  # Calling the unique() function over the# above vector to remove duplicate valuesunique(A)
Output : 
[1] 1 2 3 5 6 7
The below example shows the process to return the unique elements from the specified matrix. 
R
# R program to illustrate# unique() function # Creating a 2*5 matrix with 10df<-matrix(rep(1:5,length.out=10),           nrow = 2,ncol=5,byrow = T) print("Original df:") # Getting the matrix along with repeated# elementsdf print("After using Unique:")# Calling the unique() function to# remove the duplicate values and# Getting the matrix with unique elementsunique(df)
Output: 
[1] "Original df:"
1    2    3    4    5
1    2    3    4    5
[1] "After using Unique:"
1    2    3    4    5
The below example shows the process to return the unique elements from the specified data frame. 
R
# R program to illustrate# unique() function # Creating a data frameClass_data <- data.frame(Student = c('Aman', 'Sita',                                     'Aman', 'Rohan',                                     'Sita'),    Age = c(22, 23, 22, 22, 23), Gender = c('Male', 'Female',                                          'Male', 'Male',                                          'Female')) # Getting the data frame along with the repeated# elementsClass_data # Printing new linewriteLines("\n") # Calling the unique() function over the above# data frame to remove repeated elements and print# the unique elements onlyunique(Class_data)
Output: 
  Student Age Gender
1    Aman  22   Male
2    Sita  23 Female
3    Aman  22   Male
4   Rohan  22   Male
5    Sita  23 Female

 Student Age Gender
1    Aman  22   Male
2    Sita  23 Female
4   Rohan  22   Male
The below example shows the process to return the unique elements of a particular column from the given data frame. 
R
# R program to illustrate# unique() function # Creating a data framedata <- data.frame(x1 = c(9, 5, 6, 8, 9, 8),                     x2 = c(2, 4, 2, 7, 1, 4),                x3 = c(3, 6, 7, 0, 3, 7),                x4 = c("Hello", "value", "value",                       "geeksforgeeks", NA, "GFG")                  ) # Calling the unique() function to extract# the unique values from the particular# columns of "x1" and "x2"unique(data[c("x1")])  unique(data[c("x2")])
Output: 
  x1
1  9
2  5
3  6
4  8

 x2
1  2
2  4
4  7
5  1
The below example shows the process to return the length of the unique elements present in a given vector.
R
# R program to illustrate# unique() function # Initializing a vectordf <- c(1,2,3,4,5,6,7,4,5,6) # Calling the unique() functiondf_uniq <- unique(df) # Calling the length function to# return the length of unique values# present in the above given vectorlength(df_uniq)
Output: 
[1] 7
 
anikakapoor
Picked
R-Functions
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Change Color of Bars in Barchart using ggplot2 in R
Data Visualization in R
How to Change Axis Scales in R Plots?
Group by function in R using Dplyr
Logistic Regression in R Programming
How to Split Column Into Multiple Columns in R DataFrame?
Control Statements in R Programming
How to import an Excel File into R ?
Replace Specific Characters in String in R
How to filter R DataFrame by values in a column?