Union in R

In this chapter, let’s learn how to perform union in R for Vector and data frame. union() function in R performs union of two or more vectors or data frames.

union in R – union of vectors:

# union in R - Union of two vectors in R

x <- c(1:4)
y <- c(2:7)
union(x,y)

on execution of above code the output will be union of two vectors

[1] 1 2 3 4 5 6 7

union in R- union of data frames : 

Union of two data frames in R can be easily achieved by using merge() function. Lets see with an 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

Then merge these two data frames with an option all=TRUE as shown below

# union in R - union of data frames in R

df_union1<-merge(df1,df2,all=TRUE)
df_union1

so the resultant data frame will be

 CustomerId     Product

1          1            Oven

2          2            Oven

3          3            Oven

4          4        Television

5          5        Television

6          6        Television

7          6    Air conditioner

8          7    Air conditioner

Thus we have applied union in R for data frames

Example 2 on union of data frames in R:

There is an indirect way for union of two data frames in R. it can done In two steps.

  •    First row bind (rbind) all the data frames ·
  •   Then Remove the duplicates

These two step has to be done sequentially and has been explained with an example.

Row bind these two data frames as shown below

#row bind the data frames.

df_cat<-rbind(df1,df2)
df_cat

so the resultant df_cat data frame will be

   CustomerId     Product

1           1            Oven

2           2            Oven

3           3            Oven

4           4        Television

5           5        Television

6           6        Television

7           4        Television

8           5        Television

9           6    Air conditioner

10          7    Air conditioner

Retrieve only unique rows from the above df_cat data frame as shown below.

#unique function.

df_union <- unique(df_cat)
df_union

So the final output will be

   CustomerId    Product

1           1             Oven

2           2             Oven

3           3             Oven

4           4        Television

5           5        Television

6           6        Television

9           6    Air conditioner

10          7    Air conditioner

Thus we have learned how to apply union in R for data frames indirectly.

previous small union in r                                                                                                                next small union in r