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

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.**intersect()**

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.