Intersection of Two Objects in R Programming – intersect() Function

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

[1] 2 3 4

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

  y1
1  2
2  3
3  4



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.