Skip to content
Related Articles

Related Articles

Improve Article
Intersection of Two Objects in R Programming – intersect() Function
  • Last Updated : 15 Jun, 2020

intersect() function in R Language is used to find the intersection of two Objects. This function takes two objects like Vectors, dataframes, etc. as arguments and results in a third object with the common data of both the objects.

Syntax: intersect(x, y)

Parameters:
x and y: Objects with sequence of items

Example 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)                 

Output:

[1] 2 3 4

Example 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)                

Output:

  y1
1  2
2  3
3  4



My Personal Notes arrow_drop_up
Recommended Articles
Page :