# R Program to find elements that are present in two given data frames

March 23, 2022, Learn eTutorial
1247

## How to find elements that are present in two given data frames

Here we are explaining how to write an R program to find elements that are present in two given data frames. Here we are using a built-in function intersect(). The function intersect() helps to calculate the intersection of subsets of a probability space and the comparisons are made row-wise. The syntax of this function is,

``````
intersect(x, …)
```
```

Where x, y vectors, data frames, or ps objects containing a sequence of items. And dots(...) indicates the arguments to be passed to or from other methods.

## How to find elements that are present in two given data frames in the R program

Below are the steps used in the R program to find elements that are present in two given data frames. In this R program, we directly give the data frame to a built-in function. Here we are using variables X, Y for holding different data frames. Finally, find the common elements of the data frames by calling the function intersect() like intersect(X, Y).

## ALGORITHM

STEP 1: Assign variables X,Y with data frames

STEP 2: First print original data frames

STEP 3:  Find the common elements from the data frames by calling like intersect(X, Y)

STEP 4:  Store the result in the variable result

STEP 5: Print the final data frame

## R Source Code

```                                          ```X = c("a", "b", "c", "d", "e")
Y = c("d", "e", "f", "g")
print("Original Dataframes")
print(X)
print(Y)
print("Common elements in both dataframes:")
result = intersect(X, Y)
print(result)```
```

## OUTPUT

```[1] "Original Dataframes"
[1] "a" "b" "c" "d" "e"
[1] "d" "e" "f" "g"
[1] "Common elements in both dataframes:"
[1] "d" "e"```

VIEW ALL
VIEW ALL