## R Program to how to find common elements between two dataframe in r

### How to find elements that are common but only come once to both given data frames

Here we are explaining how to write an R program to find elements that are common but only come once to both given data frames. Here we are using a built-in function union(). The function union() helps to calculate the union of subsets of a probability space. The syntax of this function is,

union(x,y, …)

– 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 common but only come once to both given data frames in the R program

Below are the steps used in the R program to find elements that are common but only come once to both 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 but only come once to both given data frames by calling the function union() like union(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 but only come once to both the data frames by calling like union(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")
## [1] "Original Dataframes"
print(X)
## [1] "a" "b" "c" "d" "e"
print(Y)
## [1] "d" "e" "f" "g"
print("Elements that are common but only come once to both given data frames:")
## [1] "Elements that are common but only come once to both given data frames:"
result = union(X, Y)
print(result)
## [1] "a" "b" "c" "d" "e" "f" "g"

### R Source Code

X = c(1,2,3,4,5,6,7,8)
Y = c(2,4,10,12)

print("Original Dataframes")
## [1] "Original Dataframes"
print(X)
## [1] 1 2 3 4 5 6 7 8
print(Y)
## [1]  2  4 10 12
print("Elements that are common but only come once to both given data frames:")
## [1] "Elements that are common but only come once to both given data frames:"
result = union(X, Y)
print(result)
##  [1]  1  2  3  4  5  6  7  8 10 12