Skip to content

Instantly share code, notes, and snippets.

@bhive01
Created January 30, 2019 22:45
Show Gist options
  • Save bhive01/2032b90435196bbe2cc4dcfa55b093eb to your computer and use it in GitHub Desktop.
Save bhive01/2032b90435196bbe2cc4dcfa55b093eb to your computer and use it in GitHub Desktop.
library(tidyverse)
not_the_fucking_same <- function(x, y, keepNAs = "all") {
if (keepNAs == "all") {
x != y | is.na(x) | is.na(y)
} else if (keepNAs == "x") {
x != y | is.na(x)
} else if (keepNAs == "y") {
x != y | is.na(y)
} else {
stop("WTF input?")
}
}
d_f <- tibble(x = c(0,1,2,NA,4), y = c(0,NA,2,3,5))
d_f %>% filter(x != y | is.na(x) | is.na(y))
#doesn't work with NAs
d_f %>% filter(Vectorize(identical)(x,y))
d_f %>% filter(Vectorize(isTRUE)(x,y))
d_f %>% filter(not_the_fucking_same(x,y))
d_f %>% filter(not_the_fucking_same(x,y, "x"))
d_f %>% filter(not_the_fucking_same(x,y, "y"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment