library(tidyverse)
library(lubridate)
(df<- tribble(~group , ~date ,~value,
"a", " 2020-06-01", 1,
"a", " 2020-06-02", 1,
"a", " 2020-06-03", -1,
"b", " 2020-06-01", 2,
"b", " 2020-06-02", -1,
"b", " 2020-06-03", -1,
"c", " 2020-06-02", -3,
"c", " 2020-06-03", 3,
"c", " 2020-06-04", 2) %>% mutate(date=ymd(date)))
#date combinations
uniqs <- unique(df$date)
combs <- map(1:length(uniqs),
~combn(x = uniqs,
m=.,
simplify = FALSE
)) %>% flatten
solutions1 <- map(seq_along(combs),
~ {filter(df,date %in% combs[[.x]]) %>% group_by(
group) %>% summarise(value=sum(value)) %>%
filter(value==0) %>% pull(group)} %>% paste0(collapse=" & ")) %>% as.character()
combs_char <- map(combs,
~ paste(format.Date(.,"%Y-%m-%d"),collapse=" & "))
names(solutions1) <-combs_char
tidy_solutions <- enframe(solutions1)
(tidy_solutions_min <- filter(tidy_solutions,
value != ""))
# # A tibble: 6 x 2
# name value
# <chr> <chr>
# 1 2020-06-01 & 2020-06-03 a
# 2 2020-06-02 & 2020-06-03 a & c
# 3 2020-06-01 & 2020-06-02 & 2020-06-03 b & c
# 4 2020-06-01 & 2020-06-03 & 2020-06-04 a
# 5 2020-06-02 & 2020-06-03 & 2020-06-04 a
# 6 2020-06-01 & 2020-06-02 & 2020-06-03 & 2020-06-04 b