Unique Function in R

unique function in R –unique(), eliminates duplicate elements/rows from a vector, data frame or array. unique values of a vector. Unique values of a matrix and unique rows of the dataframe in R is obtained by using unique() function in R. we will looking at the following example which depicts unique() function in R.

  • unique value of a vector in R
  • distinct rows of a dataframe using unique() function in R
  • unique value a column in R.
  • Obtain the unique value of a matrix in R
  • count of distinct value of a column in R
  • count of distinct rows of the dataframe in R
  • group by count of a column in R.

unique function in R 0

 

Syntax for unique function in R:

unique(x)

if x is a vector, matrix or a data frame, returns a similar object but with the duplicate elements eliminated

 

Example of Unique function in R: unique value of a vector in R

## unique of a vector
x<-c(1:10,5:15)
unique(x)

in the above example duplicate occurrence of 5,6,7,8,9 and 10 are eliminated and made to occur only once, so the output will be

[1]  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15

 

Unique value of a Matrix in R:

First lets create the matrix in R as shown below

## unique of a column in a data frame
sample_mat = matrix(rep(1:16,length.out=24),nrow=6,ncol=4,byrow=T)
sample_mat

Matrix sample_mat is

unique function in R 1

now lets use unqiue function to extract unique values of the matrix

 
## unique value of matrix in R
unique(sample_mat)

so the unique value the matrix is

unique function in R 2

 

Unique value of a Matrix in R by keeping last occurrences:

unique() function along with the argument fromLast =T indicates keeping the last occurrence in the process of identifying unique values

 
## unique value of matrix in R by keeping last occurrences

unique(sample_mat,fromLast = T)

unique values of a matrix by keeping last occurrences will be

unique function in R 3

 

Unique function for a data frame in R: 

Unique function in R can also be applicable for data frame,  we had already  seen examples for matrices, and arrays. Let’s see an example on how to use unique function to identify unique elements in a data frame

Example: create dataframe
# simple Data frame creation

df = data.frame (NAME =c ('Alisa','Bobby','jodha','jack','raghu','Cathrine',
                      'Alisa','Bobby','kumar','Alisa','jack','Cathrine'),
                      Age = c (26,24,26,22,23,24,26,24,22,26,22,24),
                      Score =c(85,63,55,74,31,77,85,63,42,85,74,77))

df

so the resultant data frame will be
unique function in R 4

  When we apply unique function to the above data frame

## Apply unique function for data frame in R
unique(df)

Duplicate entries in the data frame are eliminated and the final output will be
unique function in R 5

 

unique rows of the dataframe by keeping last occurrences

unique() function along with the argument fromLast =T indicates keeping the last occurrence in the process of identifying unique values

 
## unique value of dataframe in R by keeping last occurrences

unique(df, fromLast=T)

unique values of a dataframe by keeping last occurrences will be

unique function in R 6

unique value of the columns in the dataframe

unique() function takes up the column name as argument and results in identifying unique value of the particular column as shown below

## unique value of the column in R dataframe

unique(df$NAME)

so the unique values of the name column will be

unique function in R 7

 

 

Get count of distinct value of column in R

unique() function along with the length() function takes up the column name as argument and gets the count of distinct value of the column in R.

###### Get count of unique values of column

length(unique(df$NAME))

Name column as 7 distinct values

output:

[1]  7

 

count of distinct value of a column by group in R :

unique() function along with the length() function and aggregate function gets the count of distinct value of a column by group

 
##### count of unique value of the group in R

aggregate(Age ~ NAME , df, function(x) length(unique(x)))

so the result will be

unique function in R 8

 

previous small unique function in r                                                                                                          next small unique function in R

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.