I have the following default basis:
df <- data.frame(
lead_15 = c(1,0,0,0,0,1,0,0,1,0,0,0,0,0,1),
lead_30 = c(0,0,0,1,0,0,1,1,0,1,0,0,0,1,0),
lead_60 = c(0,1,0,0,1,0,0,0,0,0,1,1,0,0,0),
inib_15 = c(1,0,0,0,0,0,0,0,1,0,0,0,0,0,0),
inib_30 = c(0,0,0,1,0,0,1,0,0,0,0,0,0,1,0),
inib_60 = c(0,0,0,0,1,0,0,0,0,0,1,1,0,0,0),
motivo_15 = c("A","","","","","","","","D","","","","","",""),
motivo_30 = c("","","","B","","","A","","","","","","","B",""),
motivo_60 = c("","","","","C","","","","","","B","D","","","")
)
I want a solution where there is a line for each lead (3 lines) where the first column is the sum of the respective lead, the sum of the respective inhib and a column for each reason (A, B, C, D) the amount of these reasons.
LEAD | QTD | INIB | A | B | C | D |
--------|-----|------|---|---|---|---|
lead_15 | 4 | 2 | 1 | 0 | 0 | 1 |
--------|-----|------|---|---|---|---|
lead_30 | 5 | 3 | 1 | 2 | 0 | 1 |
--------|-----|------|---|---|---|---|
lead_60 | 4 | 3 | 0 | 1 | 1 | 1 |
It's a relatively simple problem that I can solve but with many pieces of code and separate accounts. I wanted to ask here because I know there can be a direct solution using dplyr
.