Union and union_all Function in R using Dplyr (union of data frames):

Union and union_all Function in R : Union of two data frames in R can be easily achieved by using union Function and union all function in Dplyr package . Dplyr package in R is provided with union(), union_all() function.

Union function in R:

UNION function in R combines all rows from both the tables and removes duplicate records from the combined dataset

union_all function in R:

 UNION_ALL function in R combines all rows from both the tables without removing the duplicate records from the combined dataset.

Union Function in R example: First lets create two data frames

#Create two data frames

df1 = data.frame(CustomerId = c(1:6), Product = c(rep("Oven", 3), rep("Television", 3)))
df2 = data.frame(CustomerId = c(4:7), Product = c(rep("Television", 2), rep("Air conditioner", 2)))

df1 will be

    CustomerId  Product
1        1                 Oven
2        2                 Oven
3        3                 Oven
4        4                 Television
5        5                 Television
6        6                 Television

df2 will be 

CustomerId         Product

1          4           Television
2          5           Television
3          6          Air conditioner
4          7          Air conditioner

 

Union Function in R :UNION function in R combines all rows from both the tables and removes duplicate records from the combined dataset. So the resultant dataframe will not have any duplicates.

library(dplyr)

#  union two dataframes  without duplicates
union(df1,df2)

union and union_all function in R using dplyr 2

 

union_all Function in R example:UNION_ALL function in R combines all rows from both the tables without removing the duplicate records from the combined dataset. So the resultant dataframe will have duplicates.

library(dplyr)

#  union two dataframes  with duplicates
union_all(df1,df2)

union and union_all function in R using dplyr 1

 

union union_all function in R using dplyr                                                                                                               union union_all function in R using dplyr