intersect()R語言中的函數用於查找兩個對象的交集。此函數將兩個對象(如 Vectors、dataframes 等)作為參數,並生成具有兩個對象的公共數據的第三個對象。
用法: intersect(x, y)
參數:
x and y:具有項目序列的對象
範例1:
# R program to illustrate 
# intersection of two vectors 
    
# Vector 1 
x1 <- c(1, 2, 3, 4, 5, 6, 5, 5)    
    
# Vector 2  
x2 <- c(2:4)     
    
# Intersection of two vectors   
x3 <- intersect(x1, x2)       
    
print(x3)                 輸出:
[1] 2 3 4
範例2:
# R program to illustrate  
# the intersection of two data frames 
    
# Data frame 1 
data_x <- data.frame(x1 = c(2, 3, 4),     
                     x2 = c(1, 1, 1)) 
    
# Data frame 2 
data_y <- data.frame(y1 = c(2, 3, 4),        
                     y2 = c(2, 2, 2)) 
    
# Intersection of two data frames 
data_z <- intersect(data_x, data_y)   
    
print(data_z)                輸出:
y1 1 2 2 3 3 4
相關用法
- R語言 setdiff()用法及代碼示例
- R語言 setequal()用法及代碼示例
- R語言 union()用法及代碼示例
- R語言 identical()用法及代碼示例
- R語言 is.element()用法及代碼示例
- R語言 sum()用法及代碼示例
- R語言 combine()用法及代碼示例
- R語言 ls()用法及代碼示例
- R語言 rm()用法及代碼示例
- R語言 prod()用法及代碼示例
- R語言 is.primitive()用法及代碼示例
- R語言 outer()用法及代碼示例
- R語言 cov()用法及代碼示例
- R語言 cor()用法及代碼示例
- R語言 merge()用法及代碼示例
- R語言 julian()用法及代碼示例
- R語言 dunif()用法及代碼示例
注:本文由純淨天空篩選整理自nidhi_biet大神的英文原創作品 Intersection of Two Objects in R Programming – intersect() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
