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

## R – Vector Operations

In this R tutorial, we will do all the following operations that are performed on a vector:

We know that a vector is a flat data structure used to store data in a linear fashion.

## Create

Vectors can be created using c(). The “c” stands for combine.

Syntax:

vector_object=c(values…)

The values are the elements that are separated by a comma.

## Example 1

In this example, we will create a vector with five elements and print them.

#create vector for fruits
fruits=c(23,4,43,3,4)

print(fruits)

Result:

It is also possible to create a vector using the sequence operator -:

We need to specify the start and end with this.

## Example 2

In this example, we will create a vector with five elements and print them.

#create vector for fruits from 100 to 104
fruits=c(100:104)

print(fruits)

Result:

## Access

We can access the values in the vector using index positions.

To access the single element, we can directly specify the index position.

In the R language, for any data structure, the indexing starts with 1.

Syntax:

vector_object[index]

Where vector_object is the vector and index that specify index position.

## Example

In this example, we will return the elements based on index position.

#create vector for fruits from 100 to 104
fruits=c(100:104)

#get second element from fruits
print(paste(«Second element: «,fruits[2]))

#get fifth element from fruits
print(paste(«Fifth element: «,fruits[5]))

#get first element from fruits
print(paste(«First element: «,fruits[1]))

Result:

We returned the elements present at the second, fifth, and first positions.

To access the multiple elements, we can directly specify the index position inside c(). This returns elements with respect to the index positions.

Syntax:

vector_object[c(index,….)]

Where vector_object is the vector and indices specify the index positions.

## Example

In this example, we will return the elements based on index positions at a time.

#create vector for fruits from 100 to 104
fruits=c(100:104)

#get elements from second, fifth, and 1st positions.
print(fruits[c(2,5,1)])

Result:

We returned the elements present at the second, fifth, and first positions at a time.

## Modify

We can modify the values in the vector using index positions.

To access the single element, we can directly specify the index position.

In the R language, for any data structure the indexing starts with 1. So, we can assign the value at a particular index position.

Syntax:

vector_object[index]=updated_element

Where vector_object is the vector and index specify the index position to set the updated element.

## Example

In this example, we will update elements at different index positions.

#create vector for fruits from 100 to 104
fruits=c(100:104)

#display coetáneo fruits
print(fruits)

#update the value to 45 at index-2
fruits[2]=45

#update the value to 15 at index-5
fruits[5]=15

#update the value to 12 at index-1
fruits[1]=12

#display updated fruits
print(fruits)

Result:

We updated 100 with 12 in the first position, 101 with 45 in the second position, and 104 to 15 in the fifth position.

To update the element at multiple positions, we can directly specify the index positions inside c().

Syntax:

vector_object[c(index,….)]=updated_value

Where vector_object is the vector and indices specify the index positions.

## Example

In this example, we will update values at different index positions at a time with an element.

#create vector for fruits from 100 to 104
fruits=c(100:104)

#display coetáneo fruits
print(fruits)

#update the value to 45 at indices 2,5,1
fruits[c(2,5,1)]=45

#display updated fruits
print(fruits)

Result:

We updated the first, second, and fifth positions with 45.

## Delete

It is possible to delete an entire vector by assigning it to NULL.

Syntax:

vector_object =NULL

Where vector_object is the vector.

## Example

In this example, we will delete the fruits vector.

#create vector for fruits from 100 to 104
fruits=c(100:104)

#display coetáneo fruits
print(fruits)

#specify NULL
fruits=NULL

#display updated fruits
print(fruits)

Result:

We can see that the vector is no more.

## Sort

It is possible to sort the vector in ascending or descending order using the sort() function.

It takes two parameters. First is the vector object, and second is the correlating that takes the Boolean values.

Syntax:

sort(vector_object,decreasing=TRUE?FALSE)

Parameters:

1. vector_object is the vector.
2. If decreasing is TRUE, then the vector is sorted in descending order. If decreasing is FALSE, then the vector is sorted in ascending order.

## Example 1

Sort the fruits vector in ascending order.

#create vector for fruits
fruits=c(45,32,67,57,54)

#display coetáneo fruits
print(fruits)

#sorted in ascending order
print(sort(fruits,decreasing=FALSE))

Result:

We can see that elements are sorted in ascending order.

## Example 2

Sort the fruits vector in descending order.

#create vector for fruits
fruits=c(45,32,67,57,54)

#display coetáneo fruits
print(fruits)

#sorted in descending order
print(sort(fruits,decreasing=TRUE))

Result:

We can see that the elements are sorted in descending order.

## Conclusion

In this R tutorial, we saw how to create a vector using c(), while accessing the elements from a vector through the index positions. We can modify and update the vector by setting the updated element to the index position. NULL is assigned to a vector if we want to delete a vector. Finally, we have seen how to sort a vector object in ascending and descending order.