Skip to content

Instantly share code, notes, and snippets.

@nathania
Last active March 12, 2019 19:58
Show Gist options
  • Save nathania/fa7ac60110c435cfb3d7572e5931f00d to your computer and use it in GitHub Desktop.
Save nathania/fa7ac60110c435cfb3d7572e5931f00d to your computer and use it in GitHub Desktop.
generate all 2^(dim(S)) combinations of elements from a set S
# `expand(df, nesting(patient, PCP), event_type)`
# generates all unique combinations in `(patient, PCP) x event_type`.
# That is; there will be `n_distinct(df$event_type)` rows per `patient, PCP`
# tuple in the data
#
# df <- tibble(x = 1:4, y = letters[1:4])
# testthat::expect_equivalent(expand(df, nesting(x, y)), distinct(df, x, y))
# testthat::expect_equivalent(expand(df, crossing(x, y)), expand(df, x, y))
# generate all combinations of elements in a set S
dim_S <- 6
S <- letters[1:dim_S]
S_ <- rlang::rep_named(S, list(0:1)) %>% as_tibble()
expand_S <- lift_dl(partial(expand, data = S_))
combs <- expand_S(map(names(S_), sym))
combs <- combs %>%
pmap(.f = c) %>%
map(keep, .p = ~ ..1 == 1) %>%
map(names)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment