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 1: Unique elements from the specified vector
The below example shows the process to return the unique elements from the specified vector.
# 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
Example 2: Unique elements from the specified matrix
The below example shows the process to return the unique elements from the specified matrix.
# R program to illustrate # unique() function # Creating a 2*5 matrix with 10 df<- matrix ( rep (1:5,length.out=10),
nrow = 2,ncol=5,byrow = T)
print ( "Original df:" )
# Getting the matrix along with repeated # elements df print ( "After using Unique:" )
# Calling the unique() function to # remove the duplicate values and # Getting the matrix with unique elements unique (df)
|
Output:
[1] "Original df:" 1 2 3 4 5 1 2 3 4 5 [1] "After using Unique:" 1 2 3 4 5
Example 3: Unique elements from the specified dataframe
The below example shows the process to return the unique elements from the specified data frame.
# R program to illustrate # unique() function # Creating a data frame Class_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 # elements Class_data # Printing new line writeLines ( "\n" )
# Calling the unique() function over the above # data frame to remove repeated elements and print # the unique elements only unique (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
Example 4: Unique elements of a particular column from the given data frame
The below example shows the process to return the unique elements of a particular column from the given data frame.
# R program to illustrate # unique() function # Creating a data frame data <- 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
Example 5: Unique elements present in a given vector
The below example shows the process to return the length of the unique elements present in a given vector.
# R program to illustrate # unique() function # Initializing a vector df <- c (1,2,3,4,5,6,7,4,5,6)
# Calling the unique() function df_uniq <- unique (df)
# Calling the length function to # return the length of unique values # present in the above given vector length (df_uniq)
|
Output:
[1] 7