4
I have the following basis of defaulters:
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 row for each lead (3 lines) where the first column is the sum of the respective lead, sum of the respective Inib and a column for each motif (A, B, C, D) where has the amount of these motifs.
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 a lot of code pieces and separate accounts. I wanted to ask here because I know there can be a direct solution using the dplyr
.
I didn’t specify right, but what I meant was a solution with dplyr functions, so that it already built the foundation for all leads, not one at a time, full of accounts, ifs and so on, which was how I was doing... Even more pq has many other variables besides these. And I didn’t know about tidyr, it contains many functions that were what I was looking for.
– TheBiro