Skip to content

Instantly share code, notes, and snippets.

@rudeboybert
Created October 22, 2019 12:38
Show Gist options
  • Save rudeboybert/968dbf26d5f9407703ae7b24d217af67 to your computer and use it in GitHub Desktop.
Save rudeboybert/968dbf26d5f9407703ae7b24d217af67 to your computer and use it in GitHub Desktop.
Different types of dplyr joins
library(dplyr)
# Create two example data frames:
dfA <- tibble(
ID = c(76, 79, 9),
age = c(21, 23, 24)
)
dfB <- tibble(
ID = c(79, 76, 11),
state = c("MA", "OR", "DE")
)
# 1. This matches to ID's in "left" data frame i.e. dfA:
dfA %>%
left_join(dfB, by = "ID")
# 2. This matches to ID's in "right" data frame i.e. dfB:
dfA %>%
right_join(dfB, by = "ID")
# 3. This matches to ID's that exist in both data frames:
dfA %>%
inner_join(dfB, by = "ID")
# 4. This matches to ID's that exist either data frame:
dfA %>%
full_join(dfB, by = "ID")
# 5. This matches to ID's that exist in "left" data frame, but not "right" data frame:
dfA %>%
anti_join(dfB, by = "ID")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment