library(tidyverse)
DF <- tibble::tribble(
~a, ~b, ~c, ~d, ~e,
0, 0, 1, 1, 1,
0, 0, 0, 1, 0,
1, 0, 0, 0, 0,
0, 1, 1, 1, 1,
1, 0, 0, 0, 0,
0, 0, 1, 1, 1,
0, 0, 1, 1, 1,
)
h <- names(DF)
#stringrep1 proof of concept specifying a,b,c etc manually
conv <- function(x) {
ifelse(DF[[x]], x, "")
}
DF$stringrep1 <- paste0(
conv("a"),
conv("b"),
conv("c"),
conv("d"),
conv("e")
)
convx <- function(x) {
res <- list()
for (y in x)
{
res[[y]] <- paste0("conv('", y, "')")
}
res
}
##stringrep2 passing header names only
DF$stringrep2 <- eval(parse(text = paste0(
"paste0(",
paste0(
convx(h),
collapse = ","
),
")"
)))
DF2 <- DF %>%
group_by_all() %>%
count(name = "frequency_of_combination")
> DF2
# A tibble: 4 x 8
# Groups: a, b, c, d, e, stringrep1, stringrep2 [4]
a b c d e stringrep1 stringrep2 frequency_of_combination
<dbl> <dbl> <dbl> <dbl> <dbl> <chr> <chr> <int>
1 0 0 0 1 0 d d 1
2 0 0 1 1 1 cde cde 3
3 0 1 1 1 1 bcde bcde 1
4 1 0 0 0 0 a a 2