Я не уверен, что понимаю, что вам нужно, но может быть что-то вроде этого?
library(magrittr)
library(lubridate)
#>
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:base':
#>
#> date
dat <- tibble::tribble(
~barcode_no, ~Inspection_date, ~current_profile, ~score, ~Tag_log, ~prod_log,
12345678L, "15/01/2020", "Large", 10L, "C1", "WIP",
12345678L, "15/01/2020", "Medium", 20L, "C2", "Hold",
12345678L, "15/01/2020", "Medium", 50L, "C3", "Hold",
12345678L, "15/01/2020", "Medium", 70L, NA, "Shipped",
12345678L, "15/01/2020", "Medium", 120L, "C1", "Shipped",
12345678L, "15/01/2020", "Small", 150L, "C2", "Shipped",
12345678L, "15/01/2020", "Small", 10L, "C3", "WIP",
12345678L, "15/01/2020", "Small", 20L, "C2", "Hold",
12345678L, "15/01/2020", "Small", 130L, "C1", "Hold",
12345678L, "15/01/2020", "Small", 40L, NA, "Hold"
)
dat$Inspection_date = as.Date(dat$Inspection_date,format = "%d/%m/%Y")
today = Sys.Date()
param_date = as.Date("15/01/2020",format = "%d/%m/%Y")
dat$month = format(ymd(dat$Inspection_date),'%b-%Y')
dat$score_group = dplyr::case_when(
dat$score <= 50 ~ "low",
dat$score < 100 ~ "med",
TRUE ~ "high"
)
dat %>% dplyr::filter(Inspection_date >= param_date) %>%
dplyr::group_by(current_profile, month, score_group, Tag_log,prod_log) %>%
dplyr::summarise(count = dplyr::n()) %>%
tidyr::pivot_wider(names_from = c("score_group","Tag_log"),
values_from = count,
values_fill = list(count = 0)) -> res_dat
knitr::kable(res_dat,format = "markdown")
|current_profile |month |prod_log | low_C1| high_C1| low_C2| low_C3| med_NA| high_C2| low_NA|
|:---------------|:--------|:--------|------:|-------:|------:|------:|------:|-------:|------:|
|Large |Jan-2020 |WIP | 1| 0| 0| 0| 0| 0| 0|
|Medium |Jan-2020 |Shipped | 0| 1| 0| 0| 1| 0| 0|
|Medium |Jan-2020 |Hold | 0| 0| 1| 1| 0| 0| 0|
|Small |Jan-2020 |Hold | 0| 1| 1| 0| 0| 0| 1|
|Small |Jan-2020 |Shipped | 0| 0| 0| 0| 0| 1| 0|
|Small |Jan-2020 |WIP | 0| 0| 0| 1| 0| 0| 0|