Types of Tidyverse Joins
- Inner Join (inner_join()): Returns rows that have matching values in both datasets. Non-matching rows are excluded.
- Left Join (left_join()): Returns all rows from the left dataset. Includes matching rows from the right dataset. Non-matching rows in the right dataset are filled with NA values.
- Right Join (right_join()): Returns all rows from the right dataset. Includes matching rows from the left dataset. Non-matching rows in the left dataset are filled with NA values.
- Full Join (full_join()): Returns all rows from both datasets. Non-matching rows are filled with NA values.
library(dplyr)
# Example datasets
df1 <- tibble(id = c(1, 2, 3), value = c("A", "B", "C"))
df2 <- tibble(id = c(2, 3, 4), attribute = c("X", "Y", "Z"))
df1
df2
# Inner Join
inner_result <- inner_join(df1, df2, by = "id")
inner_result
# Left Join
left_result <- left_join(df1, df2, by = "id")
left_result
# Right Join
right_result <- right_join(df1, df2, by = "id")
right_result
# Full Join
full_result <- full_join(df1, df2, by = "id")
full_result
Output:
# A tibble: 3 × 2
id value
<dbl> <chr>
1 1 A
2 2 B
3 3 C
# A tibble: 3 × 2
id attribute
<dbl> <chr>
1 2 X
2 3 Y
3 4 Z
Inner Join
# A tibble: 2 × 3
id value attribute
<dbl> <chr> <chr>
1 2 B X
2 3 C Y
Left Join
# A tibble: 3 × 3
id value attribute
<dbl> <chr> <chr>
1 1 A NA
2 2 B X
3 3 C Y
Right Join
# A tibble: 3 × 3
id value attribute
<dbl> <chr> <chr>
1 2 B X
2 3 C Y
3 4 NA Z
Full Join
# A tibble: 4 × 3
id value attribute
<dbl> <chr> <chr>
1 1 A NA
2 2 B X
3 3 C Y
4 4 NA Z
Tidyverse joins in R
Data manipulation is a crucial aspect of data analysis and plays a significant role in deriving insights from datasets. The Tidyverse package in R provides a suite of tools for data manipulation, including powerful functions for joining datasets. In this article, we’ll explore Tidyverse joins, which allow us to combine datasets based on common columns in the R Programming Language.
Contact Us