Last active
August 22, 2024 13:32
-
-
Save xiesixia/d10befd1ce6f0186a2f7cdc6537f34c1 to your computer and use it in GitHub Desktop.
Coalesce join function applied to data frame (expand coalesce function from dplyr)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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