0
0
R Programmingprogramming~10 mins

join functions (left_join, inner_join) in R Programming - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - join functions (left_join, inner_join)
Start with two tables
Choose join type
left_join
Match keys in both tables
left_join: keep all from left, add matches
inner_join: keep only matches
Resulting table
End
Start with two tables, pick join type (left or inner), match keys, then produce a new table with rows combined accordingly.
Execution Sample
R Programming
library(dplyr)
left_join(df1, df2, by = "id")
inner_join(df1, df2, by = "id")
Joins two data frames df1 and df2 by the 'id' column using left and inner joins.
Execution Table
StepActiondf1 rowsdf2 rowsJoin TypeMatching KeysResult RowsResult Description
1Start with df1 and df232---df1 has 3 rows, df2 has 2 rows
2Perform left_join by 'id'32left_joinid=1,23All df1 rows kept; matched rows from df2 added; unmatched df2 rows ignored
3Perform inner_join by 'id'32inner_joinid=1,22Only rows with matching 'id' in both tables kept
4End of joins-----Joins complete, results ready
💡 Joins stop after matching keys and producing combined tables.
Variable Tracker
VariableStartAfter left_joinAfter inner_join
df1_rows333
df2_rows222
result_rows-32
Key Moments - 2 Insights
Why does left_join keep more rows than inner_join?
Because left_join keeps all rows from the left table (df1) even if there is no match in df2, as shown in execution_table row 2, while inner_join keeps only rows with matching keys in both tables (row 3).
What happens to rows in df2 that don't match any in df1 during left_join?
They are ignored and not included in the result, as left_join only keeps all rows from df1 and matches from df2, shown in execution_table row 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, how many rows does the result have after left_join?
A3
B2
C5
D1
💡 Hint
Check the 'Result Rows' column in execution_table row 2 for left_join.
At which step does the join keep only matching rows from both tables?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Join Type' and 'Result Description' columns in execution_table row 3.
If df2 had an extra row with id=3, how would left_join result rows change?
AResult rows would increase by 1
BResult rows stay the same
CResult rows decrease
DResult rows become zero
💡 Hint
Left_join keeps all rows from df1; extra df2 rows without matching keys do not add rows, see key_moments explanation.
Concept Snapshot
join functions (left_join, inner_join):
- left_join keeps all rows from left table + matched rows from right
- inner_join keeps only rows with matching keys in both tables
- Use 'by' to specify key columns
- Result rows differ: left_join >= inner_join
- Useful for combining related data with different inclusion rules
Full Transcript
This visual execution shows how left_join and inner_join work in R using dplyr. We start with two tables, df1 and df2, each with some rows. The left_join keeps all rows from df1 and adds matching rows from df2 based on the 'id' column. If df2 has no matching row, the left_join result still keeps the df1 row with missing values for df2 columns. The inner_join keeps only rows where both df1 and df2 have matching 'id' values, so the result has fewer rows. The execution table tracks these steps, showing the number of rows before and after each join. The variable tracker shows how the number of rows changes. Key moments clarify why left_join keeps more rows and what happens to unmatched rows in df2. The quiz tests understanding of these concepts by asking about result row counts and join behavior. This helps beginners see exactly how these joins combine data step-by-step.