Skip to content

Instantly share code, notes, and snippets.

@knbknb
Last active June 3, 2023 12:45
Show Gist options
  • Save knbknb/33769b2cfb5b50c53aa91cd9dc705899 to your computer and use it in GitHub Desktop.
Save knbknb/33769b2cfb5b50c53aa91cd9dc705899 to your computer and use it in GitHub Desktop.
R: print a basic truth table, with some custom logical predicates in mutate() call
library(tidyverse)
tt <- tibble(
x = c(TRUE, TRUE, FALSE, FALSE),
y = c(TRUE, FALSE, TRUE, FALSE)
)
`->` <- function(x, y) {
as.logical(dplyr::case_when(
x == TRUE & y == TRUE ~ TRUE,
x == TRUE & y == FALSE ~ FALSE,
x == FALSE & y == TRUE ~ TRUE,
x == FALSE & y == FALSE ~ TRUE,
TRUE ~ NA_integer_
))
}
(tt <- tt %>%
mutate("x -> y" = `->`(x, y), # predicate1
"-x" = !x,
"-x&y" = `-x` & y, # predicate2
"(x->y)v(-x&y)" = `->`(x,y) | `-x&y`)) # predicate1 | predicate2
# ...here you can add your own predicates or predicate comparisons
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment