intersect in R: Get the intersection of two objects in R

intersect in r

In this guide, we will share with you how to get the intersection of two objects in R by the intersect() function. The intersect in R is used with two common mode objects. Let’s learn more about it with the explanation and examples below.

What does the intersect do in R?

The intersect() function in R is used to get the intersection of two common mode objects. Because of the function of the intersect() in R, it is created by many packages such as: ‘dplyr’, ‘lubridate’, ‘base R’, and ‘generics’. For your demand, this function can be used in some different ways, but the main function of it is to get the intersection of the elements of two objects. Let’s take a look at the syntax of this function.

Syntax:

intersect(data1, data2)

Parameters:

  • data1: The data of the first object.
  • data2: The data of the second object.

After learning the usage and the syntax of the intersect() function, we will show you how to use this function with some examples in the next title below.

How to use the intersect in R?

The intersect() function is used with almost all objects in R such as: the vector, the array, the data frame, … In this title, we will show you how to use the intersect() function with the vector and the data frame.

Use the intersect() function with the vector

We can use the intersect() function to get the common elements of two vectors in R.

Look at the example below:

vec1 <- c(1, 2, 3, 4, 5)
vec2 <- c(3, 4, 5, 6, 7, 8)

# Get the common values of two vectors
intersect(vec1, vec2)

Output

[1] 3 4 5

Use the intersect() function with the data frame

We can use the intersect() function to get the common elements of two data frames in R.

To that, you can not use the intersect() function in the ‘base R’ package, you should use the intersect() function in the 'dplyr' instead.

Look at the example below:

# Create the first data frame
df1 <- data.frame(
    Customers = c("Alex", "Peter", "Linda", "John", "Puth"),
    Height = c(180, 190, 185, 199, 200)
)

df1

Output

    Customers Height
1      Alex    180
2     Peter    190
3     Linda    185
4      John    199
5      Puth    200

Create another data frame.

# Create the second data frame
df2 <- data.frame(
    Customers = c("Florentino", "Peter", "Linda", "John", "Tulen"),
    Height = c(180, 190, 185, 199, 201)
)

df2

Output

    Customers Height
1 Florentino    180
2      Peter    190
3      Linda    185
4       John    199
5      Tulen    201

Let’s get the common rows of two data frames you have created by the intersect() function in the ‘dplyr’ package.

df1 <- data.frame(
    Customers = c("Alex", "Peter", "Linda", "John", "Puth"),
    Height = c(180, 190, 185, 199, 200)
)

df2 <- data.frame(
    Customers = c("Florentino", "Peter", "Linda", "John", "Tulen"),
    Height = c(180, 190, 185, 199, 201)
)

# Get the common rows of two data frames
df <- dplyr::intersect(df1, df2)
df

Output

    Customers Height
1     Peter    190
2     Linda    185
3      John    199

You can learn how to merge two data frames by row names in R here.

Summary

You have learned about the usage, the syntax, and how to use the intersect in R. The intersect() function is built in some packages in R. You can use the intersect() function in different ways with the different packages to accommodate your demand. We hope this tutorial is helpful to you. Thanks!

Maybe you are interested:

Posted in R

Leave a Reply

Your email address will not be published. Required fields are marked *