R

# How to Perform Intersect() Operations in R

There is a requirement to return only the common values from R data structures like vector, list, and dataframe.

In this article, we will discuss how to perform the intersect() operations in vector, list and dataframe.

## In a Vector

The intersect() method is used to return the common values from the two vectors.

There are three ways to use the intersect() method:

1. We can directly use the intersect() method using the following command:
Syntax:

intersect(vector_object1,vector_object2)

2. We need to load the dplyr library that supports the intersect() method.
Syntax:

library(dplyr)
intersect(vector_object1,vector_object2)

It takes two vectors as the parameters.

3. Use the intersect() method with Reduce.

In this case, we can perform the intersect operation on multiple vectors.
Reduce takes two parameters. The first parameter is the intersect method and the second parameter takes the multiple vectors inside the list() function.

Syntax:
Reduce(intersect,list(vector_object1,vector_object2,……….))

Parameters:

1. intersect is the method to perform intersection.
2. vector_object represents the vector.

Example 1:

In this example, we perform an intersect() operation directly on two vectors.

#create apple price vector
apple=c(23,43,45,43,34)

#create mango price vector
mango=c(23,43,67,43,56)

#display
print(apple)
print(mango)

#do the intersection operation on apple and mango vectors
print("Intersection of apple and mango:" )

print(intersect(apple,mango))

Result:

In both vectors, 23 and 43 are common.

Example 2:

In this example, we perform an intersect() operation from the dplyr library on two vectors.

library(dplyr)

#create apple price vector
apple=c(23,43,45,43,34)

#create mango price vector
mango=c(23,43,67,43,56)

#display
print(apple)
print(mango)

#do the intersection operation on apple and mango vectors
print("Intersection of apple and mango:" )

print(intersect(apple,mango))

Result:

In both vectors, 23 and 43 are common.

Example 3:

In this example, we perform an intersect() operation using Reduce on four vectors.

#create apple price vector
apple=c(23,43,45,43,34)

#create mango price vector
mango=c(23,43,67,43,56)

#create papaya price vector
papaya=c(45,43,34)

#create peas price vector
peas=c(23,43)

#display
print(apple)
print(mango)
print(papaya)
print(peas)

#do the intersection operation on four vectors
print("Intersection of apple,mango,papaya,peas:" )

print(Reduce(intersect,list(apple,mango,papaya,peas)))

Result:

In the four vectors, 43 is common.

## In a List

The intersect() method is used to return the common values from two lists.

There are three ways to use the intersect() method:

1. We can directly use intersect() method using the following command:
Syntax:

intersect(list_object1,list_object2)

2. We need to load the dplyr library that supports the intersect() method.
Syntax:

library(dplyr)
intersect(list_object1,list_object2)

It takes two lists as parameters.

3. Use the intersect() method with Reduce.

In this case, we can perform the intersect operation on multiple lists.
Reduce takes two parameters. The first parameter is the intersect method and the second parameter takes the multiple lists inside the list() function.

Syntax:

Reduce(intersect,list(list_object1,list_object2,……….))

Parameters:

1. intersect is the method to perform intersection
2. list_object represents the vector

Example 1:

In this example, we perform an intersect() operation directly on two lists.

#create apple price list
apple=list(23,43,45,43,34)

#create mango price list
mango=list(23,43,67,43,56)

#do the intersection operation on apple and mango list
print("Intersection of apple and mango:" )

print(intersect(apple,mango))

Result:

In both lists, 23 and 43 are common.

Example 2:

In this example, we perform an intersect() operation from the dplyr library on two lists.

library(dplyr)

#create apple price list
apple=list(23,43,45,43,34)

#create mango price list
mango=list(23,43,67,43,56)

#do the intersection operation on apple and mango list
print("Intersection of apple and mango:" )

print(intersect(apple,mango))

Result:

Example 3:

In this example, we perform an intersect() operation using Reduce on four lists.

#create apple price list
apple=list(23,43,45,43,34)

#create mango price list
mango=list(23,43,67,43,56)

#create papaya price list
papaya=list(45,43,34)

#create peas price list
peas=list(23,43)

#do the intersection operation on four lists
print("Intersection of apple,mango,papaya,peas:" )

print(Reduce(intersect,list(apple,mango,papaya,peas)))

Result:

In the four lists, 43 is common.

## In a Dataframe

The intersect() method is used to return the common values from the two dataframes.

There are three ways to use the intersect() method:

1. We can directly use intersect() method using the following command:
Syntax:

intersect(dataframe_object1,dataframe_object2)

2. We need to load the dplyr library that supports the intersect() method.
Syntax:

library(dplyr)
intersect(dataframe_object1,dataframe_object2)

It takes two dataframes as parameters.

3. Use the intersect() method with Reduce.

In this case, we can perform the intersection operation on multiple dataframes.

Reduce takes two parameters. The first parameter is the intersect method and the second parameter takes the multiple dataframes inside the list() function.

Syntax:

Reduce(intersect,list(dataframe_object1,dataframe_object2,……….))

Parameters:

1. intersect is the method to perform intersection
2. dataframe_object represents the dataframe

Example 1:

In this example, we perform an intersect() operation directly on two dataframes.

#create a dataframe-market1 that has 3 rows and 5 columns.
market1=data.frame(market_id=c(1,2,3), market_name=c('M1','M2','M4'), market_place=c('India','USA','India'), market_type=c('bar','grocery','restaurent'), market_squarefeet=c(120,342,220))

#create a dataframe-market2 that has 4 rows and 5 columns.
market2=data.frame(market_id=c(1,2,3,4), market_name=c('M1','M2','M3','M4'), market_place=c('India','USA','India','Australia'), market_type=c('bar','bar','grocery','restaurent'), market_squarefeet=c(120,342,220,110))

#perform intersection on market1 and market2
print("intersection on market1 and market2")
print(intersect(market1,market2))

Result:

In both dataframes, only the 1st row is common.

Example 2:

In this example, we perform an intersect() operation directly on two dataframes.

library(dplyr)

#create a dataframe-market1 that has 3 rows and 5 columns.
market1=data.frame(market_id=c(1,2,3), market_name=c('M1','M2','M4'), market_place=c('India','USA','India'), market_type=c('bar','grocery','restaurent'), market_squarefeet=c(120,342,220))

#create a dataframe-market2 that has 4 rows and 5 columns.
market2=data.frame(market_id=c(1,2,3,4), market_name=c('M1','M2','M3','M4'), market_place=c('India','USA','India','Australia'), market_type=c('bar','bar','grocery','restaurent'), market_squarefeet=c(120,342,220,110))

#perform intersection on market1 and market2
print("intersection on market1 and market2")
print(intersect(market1,market2))

Result:

In both dataframes, only the 1st row is common.

Example 3:

In this example, we perform an intersect() operation using Reduce on three dataframes.

#create a dataframe-market1 that has 3 rows and 5 columns.
market1=data.frame(market_id=c(1,2,3), market_name=c('M1','M2','M4'), market_place=c('India','USA','India'), market_type=c('bar','grocery','restaurent'), market_squarefeet=c(120,342,220))

#create a dataframe-market2 that has 4 rows and 5 columns.
market2=data.frame(market_id=c(1,2,3,4), market_name=c('M1','M2','M3','M4'), market_place=c('India','USA','India','Australia'), market_type=c('bar','bar','grocery','restaurent'), market_squarefeet=c(120,342,220,110))

#create a dataframe-market3 that has 4 rows and 5 columns.
market3=data.frame(market_id=c(1,2,3,4), market_name=c('M1','M2','M3','M4'), market_place=c('India','USA','India','Australia'), market_type=c('bar','bar','grocery','restaurent'), market_squarefeet=c(120,342,220,110))

#perform intersection on market1, market2 and market3
print("intersection on market1,market2 and market3")
print(Reduce(intersect,list(market1,market2,market3)))

Result:

In the three dataframes, only the 1st row is common.

## Conclusion

In this R tutorial, we saw the different ways to perform the intersect() operations in the vector, list, and dataframe. If you want to perform the intersection operation on more than two data, you can use the Reduce() function.