Есть несколько способов решения этой проблемы, один из которых будет начинаться с очистки ваших данных, объединения их в таблицу, содержащую все комбинации, которые вы явно хотите, и затем суммирования. Примечание: это даст много явных нулей из-за комбинации комбинаций из этих трех столбцов.
df = df_original %>%
mutate(marking = if_else(str_detect(marking,"hold"),"Hold", marking)) %>%
mutate_at(vars(c("seq", "batch_no", "marking")), forcats::fct_explicit_na, na_level = "(Blank)")
## You need to do something similar with vectors of the possible values
## i.e. I don't know all the levels of your factors
#--------------------------------------------------------------------------
# Appending the NA and (Blank) levels ensures they are included in case the
# batch of data doesn't have them
df_seq = data.frame(seq = c(df$seq %>% levels(),"NA","(Blank)") %>% unique())
df_batch_no = data.frame(batch_no = c(df$batch_no %>% levels(),"NA","(Blank)") %>% unique())
df_marking = data.frame(marking = c(df$marking %>% levels(),"NA","(Blank)") %>% unique())
# would have been really nice to use janitor::tabyl but your output won't allow
df_seq_summary = df %>%
group_by(seq) %>%
summarise(count = n()) %>%
right_join(df_seq, by = "seq") %>%
mutate(count = replace_na(count, 0),
percentage = count / n()) %>%
mutate(row = row_number())
df_marking_summary = df %>%
group_by(marking) %>%
summarise(count = n()) %>%
right_join(df_marking, by = "marking") %>%
mutate(count = replace_na(count, 0),
percentage = count / sum(count)) %>%
mutate(row = row_number())
df_batch_no_summary = df %>%
group_by(batch_no) %>%
summarise(count = n()) %>%
right_join(df_batch_no, by = "batch_no") %>%
mutate(count = replace_na(count, 0),
percentage = count / sum(count)) %>%
mutate(row = row_number())
df = df_seq_summary %>%
full_join(df_marking_summary, by = "row", suffix = c("", "_marking")) %>%
full_join(df_batch_no_summary, by = "row", suffix = c("", "_batch_no")) %>%
select(-row) %>%
bind_rows(summarise_all(., ~(if(is.numeric(.)) sum(if_else(.>0,as.double(.),0), na.rm = T) else "Total"))) %>%
mutate_at(vars(contains("percentage")), scales::percent, accuracy = 0.01)