Skip to content

Instantly share code, notes, and snippets.

@seasmith
Created October 15, 2020 20:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seasmith/33d9b2612df792e42fbec77e4c104333 to your computer and use it in GitHub Desktop.
Save seasmith/33d9b2612df792e42fbec77e4c104333 to your computer and use it in GitHub Desktop.
Extra SQL-like functions for dplyr
# Symmetrical set-difference
# from: https://github.com/tidyverse/dplyr/issues/4811
symdiff <- function(x, y) {
setdiff(union(x, y), intersect(x, y))
}
# Inserting join
# from: https://github.com/tidyverse/dplyr/issues/1275
insert <- function(data, insertData, by) {
data %>%
dplyr::anti_join(insertData, by = by) %>%
dplyr::bind_rows(insertData) %>%
dplyr::arrange_(.dots = by)
}
# Symmetrical anti-join
sym_anti_join <- function (x, y, by = NULL, copy = FALSE, ...) {
x_only <- anti_join(x, y, by = by, copy = copy, ...)
y_only <- anti_join(y, x, by = by, copy = copy, ...)
bind_rows(x_only, y_only)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment