Was it helpful?

Question

How to do an inner join and outer join of two data frames in R?

R ProgrammingServer Side ProgrammingProgramming

An inner join return only the rows in which the left table have matching keys in the right table and an outer join returns all rows from both tables, join records from the left which have matching keys in the right table. This can be done by using merge function.

Example

Inner Join

> df1 = data.frame(CustomerId = c(1:5), Product = c(rep("Biscuit", 3), rep("Cream", 2)))
> df1
  CustomerId Product
1 1 Biscuit
2 2 Biscuit
3 3 Biscuit
4 4 Cream
5 5 Cream
> df2 = data.frame(CustomerId = c(2, 5, 6), City = c(rep("Chicago", 2),
rep("NewYorkCity", 1)))
> df2
CustomerId City
1 2 Chicago
2 5 Chicago
3 6 NewYorkCity

Inner Join

> merge(x = df1, y = df2)
  CustomerId Product City
1 2 Biscuit Chicago
2 5 Cream Chicago

Outer Join

> merge(x = df1, y = df2, by = "CustomerId", all = TRUE)
  CustomerId Product City
1 1 Biscuit <NA>
2 2 Biscuit Chicago
3 3 Biscuit <NA>
4 4 Cream <NA>
5 5 Cream Chicago
6 6 <NA> NewYorkCity
raja
Published on 06-Jul-2020 18:29:23
Advertisements
Was it helpful?
Not affiliated with Tutorialspoint
scroll top