Skip to content

Instantly share code, notes, and snippets.

@ZeccaLehn
Last active May 3, 2017 00:36
Show Gist options
  • Save ZeccaLehn/81e32b3d0c5a71f87ef76b0c97d99acb to your computer and use it in GitHub Desktop.
Save ZeccaLehn/81e32b3d0c5a71f87ef76b0c97d99acb to your computer and use it in GitHub Desktop.
Set logic in R
# Starting Order
> c(1, 2) %in% c(1,3,5) # Indexes left side
[1] TRUE FALSE
> c(1, 2) == c(1,3,5) # Same as above, but indexes right side
[1] TRUE FALSE FALSE
> ! c(1, 2) %in% c(1,3,5)
[1] FALSE TRUE
> union(c(1, 2), c(1,3,5))
[1] 1 2 3 5
> intersect(c(1, 2), c(1,3,5))
[1] 1
> setdiff(c(1, 2), c(1,3,5))
[1] 2
> setequal(c(1, 2), c(1,3,5))
[1] FALSE
> setequal(c(1, 2), c(1,2))
[1] TRUE
# Reversed Order
> c(1,3,5) %in% c(1, 2) # Indexes left side
[1] TRUE FALSE FALSE
> c(1,3,5) == c(1, 2) # Note: Indexes same left side -- different than first example
[1] TRUE FALSE FALSE
> ! c(1,3,5) %in% c(1, 2)
[1] FALSE TRUE TRUE
> union(c(1,3,5), c(1, 2))
[1] 1 3 5 2
> intersect(c(1,3,5), c(1, 2))
[1] 1
> setdiff(c(1,3,5), c(1, 2))
[1] 3 5
> setequal(c(1,3,5), c(1, 2))
[1] FALSE
> setequal(c(1,3,5), c(1,3,5))
[1] TRUE
@ZeccaLehn
Copy link
Author

For Outersect https://www.r-bloggers.com/outersect-the-opposite-of-rs-intersect-function/

outersect <- function(x, y) {
sort(c(setdiff(x, y),
setdiff(y, x)))
}

x = letters[1:3]
#[1] "a" "b" "c"
y = letters[2:4]
#[1] "b" "c" "d"

outersect(x, y)
#[1] "a" "d"

outersect(y, x)
#[1] "a" "d"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment