📜  在 R 编程中从对象中删除重复元素 – unique()函数

📅  最后修改于: 2022-05-13 01:54:43.537000             🧑  作者: Mango

在 R 编程中从对象中删除重复元素 – unique()函数

R 语言中的unique()函数用于从向量、数据框或数组中删除重复的元素/行。

示例 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