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
# 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
# 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
# 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
# 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
# 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
Similar Reads
sum() function in R sum() function in R Programming Language returns the addition of the values passed as arguments to the function. Syntax: sum(...) Parameters: ...: numeric or complex or logical vectorssum() Function in R ExampleR program to add two numbersHere we will use sum() functions to add two numbers. R a1=c(1
2 min read
which() Function in R which() function in R Programming Language is used to return the position of the specified values in the logical vector. Syntax: which(x, arr.ind, useNames) Parameters: This function accepts some parameters which are illustrated below: X: This is the specified input logical vectorArr.ind: This param
3 min read
Tidyverse Functions in R Tidyverse is a collection of R packages designed to make data analysis easier, more intuitive, and efficient. Among the various packages within Tidyverse, several key functions stand out for their versatility and usefulness in data manipulation tasks. In this article, we'll explore some of the most
4 min read
Outer() Function in R A flexible tool for working with matrices and vectors in R is the outer() function. It enables you to create a new matrix or array by applying a function to every conceivable combination of the items from two input vectors. outer() function in R Programming Language is used to apply a function to tw
3 min read
by() Function in R R has gained popularity for statistical computing and graphics. It provides the means of shifting the raw data into readable final results. in this article, we will discuss what is by() Function in R and how to use this. What is by() Function in R?The by() function is a localized function in R Progr
5 min read
map() Function in R In R Programming Language the Map function is a very useful function used for element-wise operations across vectors or lists. This article will help show how to use it with multiple code examples. Map Function in RThe Map function in R belongs to the family of apply functions, designed to make oper
3 min read
Invisible() Function in R The invisible() function in R Programming Language is used to make the printing statements invisible which means the printing statements will not appear. Syntax: invisible(data) where data is the input data structure/variable. Example 1: In this example, we are going to create two vectors and displa
1 min read
as.numeric() Function in R The as.numeric() function in R is a crucial tool for data manipulation, allowing users to convert data into numeric form, which is essential for performing mathematical operations and statistical analysis. Overview of the as.numeric() FunctionThe as. numeric() function is part of R's base package an
3 min read
Interactive() Function in R In this article, we are going to see interactive() Function in R Programming Language. Interactive() Function It returns TRUE when R is being used interactively and FALSE otherwise. This function is used to test whether R runs interactively or not. Syntax: interactive()  It will return TRUE, if is
1 min read
Windows Function in R using Dplyr Aggregation functions in R are used to take a bunch of values and give us output as a single value. Some of the examples of aggregation methods are the sum and mean. Windows functions in R provide a variation to the aggregation methods in the sense that they return the number of outputs equivalent t
7 min read