Last active
June 3, 2023 12:45
-
-
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
This file contains hidden or 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
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