Unique() Function in R
Last Updated :
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.
Example 1: Unique elements from the specified vector
The below example shows the process to return the unique elements from the specified vector.
R
A <- c (1, 2, 3, 3, 2, 5, 6, 7, 6, 5)
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
df<- matrix ( rep (1:5,length.out=10),
nrow = 2,ncol=5,byrow = T)
print ( "Original df:" )
df
print ( "After using Unique:" )
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
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' ))
Class_data
writeLines ( "\n" )
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
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" )
)
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
df <- c (1,2,3,4,5,6,7,4,5,6)
df_uniq <- unique (df)
length (df_uniq)
|
Output:
[1] 7
Share your thoughts in the comments
Please Login to comment...