Skip to content

Instantly share code, notes, and snippets.

@xiesixia
Last active May 19, 2023 06:51
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xiesixia/d10befd1ce6f0186a2f7cdc6537f34c1 to your computer and use it in GitHub Desktop.
Save xiesixia/d10befd1ce6f0186a2f7cdc6537f34c1 to your computer and use it in GitHub Desktop.
Coalesce join function applied to data frame (expand coalesce function from dplyr)
# For detailed explaination of this function, check this blog post: https://asterhu.com/post/2023-05-11-coalesce-join-in-r/
# Reference: https://alistaire.rbind.io/blog/coalescing-joins/
require(dplyr)
require(stringr)
coalesce_join <- function(x, y,
by = NULL,
keep = c("left", "right"), # "left" means keep value from left table if there's any duplicate value in both tables.
suffix = c(".x", ".y"),
join = c("full_join","left_join", "right_join", "inner_join")) {
keep = match.arg(keep)
join = match.arg(join)
join = match.fun(join)
if (keep == "left") suffix_ = suffix else suffix_ = rev(suffix)
join(x, y, by = by, suffix = suffix) %>%
mutate(
across(ends_with(suffix_[1]),
~coalesce(.,
get(str_replace(cur_column(), suffix_[1], suffix_[2]))
),
.names = "{str_remove(.col, suffix_[1])}"
),
.keep = "unused")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment