- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to do an inner join and outer join of two data frames in R?
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
- Related Articles
- Difference Between Inner Join and Outer Join in SQL
- INNER JOIN vs FULL OUTER JOIN vs LEFT JOIN vs RIGHT JOIN in PostgreSQL?
- Difference between Inner and Outer join in SQL
- Usage and syntax of INNER and OUTER JOIN in DB2
- How to join two data frames with the same row order using dplyr in R?
- How can we distinguish between MySQL CROSS JOIN and INNER JOIN?
- How to join two data frames based one factor column with different levels and the name of the columns in R using dplyr?
- Full outer join in PySpark dataframe
- How can we convert subqueries to INNER JOIN?
- Difference Between Left, Right and Full Outer Join
- Compare two tables and return missing ids using MySQL LEFT OUTER JOIN
- How to perform cartesian join for two data.table objects in R?
- How can you perform inner join on two tables using MySQL in Python?
- Python - Merge Pandas DataFrame with Outer Join
- How to join two lists in C#?

Advertisements