R语言 删除对象中的重复元素 – unique()函数
R语言中的 unique() 函数用于从一个向量、数据框或数组中删除重复的元素/行。
语法: unique(x)
参数:
x: 向量、数据框、数组或NULL
例子1 :
# R program to illustrate
# unique function
# Initializing some set of numbers
x <- c(1:10, 5:9)
x
# Calling unique() function to print
# unique set of numbers
unique(x)
输出
[1] 1 2 3 4 5 6 7 8 9 10 5 6 7 8 9
[1] 1 2 3 4 5 6 7 8 9 10
例2 :
# R program to illustrate
# unique function
# Initializing a matrix
x <- matrix(rep(1:9, length.out = 18),
nrow = 6, ncol = 3, byrow = T)
x
# Calling unique() function to print
# unique set of numbers in the matrix
unique(x)
输出
[, 1] [, 2] [, 3]
[1, ] 1 2 3
[2, ] 4 5 6
[3, ] 7 8 9
[4, ] 1 2 3
[5, ] 4 5 6
[6, ] 7 8 9
[, 1] [, 2] [, 3]
[1, ] 1 2 3
[2, ] 4 5 6
[3, ] 7 8 9