R 中的 Unique()函数
R 编程语言中的Unique()函数用于返回没有任何重复元素/行的向量、数据框或数组。
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.
示例 1:来自指定向量的唯一元素
下面的示例显示了从指定向量返回唯一元素的过程。
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)
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)
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)
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")])
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 lenght of unique values
# present in the above given vector
length(df_uniq)
输出 :
[1] 1 2 3 5 6 7
示例 2:来自指定矩阵的唯一元素
下面的示例显示了从指定矩阵返回唯一元素的过程。
电阻
# 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)
输出:
[1] "Original df:"
1 2 3 4 5
1 2 3 4 5
[1] "After using Unique:"
1 2 3 4 5
示例 3:来自指定数据帧的唯一元素
以下示例显示了从指定数据框中返回唯一元素的过程。
电阻
# 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)
输出:
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
示例 4:给定数据框中特定列的唯一元素
下面的示例显示了从给定数据框中返回特定列的唯一元素的过程。
电阻
# 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")])
输出:
x1
1 9
2 5
3 6
4 8
x2
1 2
2 4
4 7
5 1
示例 5:给定向量中存在的唯一元素
下面的示例显示了返回给定向量中存在的唯一元素的长度的过程。
电阻
# 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 lenght of unique values
# present in the above given vector
length(df_uniq)
输出:
[1] 7