I so have the following data frame
| customerid | payment_month | payment_date | bill_month | charges |
|---|---|---|---|---|
| 1 | January | 22 | January | 30 |
| 1 | February | 15 | February | 21 |
| 1 | March | 2 | March | 33 |
| 1 | May | 4 | April | 43 |
| 1 | May | 4 | May | 23 |
| 1 | June | 13 | June | 32 |
| 2 | January | 12 | January | 45 |
| 2 | February | 15 | February | 56 |
| 2 | March | 2 | March | 67 |
| 2 | April | 4 | April | 65 |
| 2 | May | 4 | May | 54 |
| 2 | June | 13 | June | 68 |
| 3 | January | 25 | January | 45 |
| 3 | February | 26 | February | 56 |
| 3 | March | 30 | March | 67 |
| 3 | April | 1 | April | 65 |
| 3 | June | 1 | May | 54 |
| 3 | June | 1 | June | 68 |
(the id data is much larger) I want to calculate payment efficiency using the following function,
efficiency = (amount paid not late / total bill amount)*100
not late is paying no later than the 21st day of the bill's month. (paying January's bill on the 22nd of January is considered as late)
I want to calculate the efficiency of each customer with the expected output of
| customerid | effectivity |
|---|---|
| 1 | 59.90 |
| 2 | 100 |
| 3 | 37.46 |
I have tried using the following code to calculate for one id and it works. but I want to apply and assign it to the entire group id and summarize it into 1 column (effectivity) and 1 row per ID. I have tried using group by, aggregate and ifelse functions but nothing works. What should I do?
df1 <- filter(df, (payment_month!=bill_month & id==1) | (payment_month==bill_month & payment_date > 21 & id==1) )
df2 <-filter(df, id==1001)
x <- sum(df1$charges)
x <- sum(df2$charges)
100-(x/y)*100