R语言 获取两个对象之间的独占元素 – setdiff()函数

R语言 获取两个对象之间的独占元素 – setdiff()函数

R编程语言 中的setdiff()函数 用于查找在第一个对象中但不在第二个对象中的元素。

语法: setdiff(x, y)

参数

  • x和y: 具有项目序列的对象

R – setdiff() 函数示例

例1:在R语言中对数字矢量应用setdiff

# R program to illustrate
# the use of setdiff() function
   
# Vector 1
x1 <- c(1, 2, 3, 4, 5, 6, 5, 5)   
   
# Vector 2 
x2 <- c(2:4)    
   
# Calling setdiff() Function
x3 <- setdiff(x1, x2)      
   
print(x3)                
R

输出

[1] 1 5 6
R

例2:在R语言中对字符矢量应用setdiff

# R program to illustrate
# the use of setdiff() function
   
# Vector 1
x <- c("GFG", "GEEKS")   
   
# Vector 2 
y <- c("GFG", "Welcome", "HOME")    
   
# Calling setdiff() Function
x3 <- setdiff(x, y)      
   
print(x3)                
R

输出

[1] "GEEKS"
R

例3:R数据帧之间的setdiff

# R program to illustrate 
# the use of setdiff() function
   
# Data frame 1
data_x <- data.frame(x1 = c(5, 6, 7),    
                     x2 = c(2, 2, 2))
   
# Data frame 2
data_y <- data.frame(y1 = c(2, 3, 4),       
                     y2 = c(2, 2, 2))
   
# Calling setdiff() Function
data_z <- setdiff(data_x, data_y)  
   
print(data_z)
R

输出

  x1
1  5
2  6
3  7
R

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册