Given data:
| grp | data1 | data2 | data3 |
|---|---|---|---|
| a | 2 | 1 | 2 |
| a | 4 | 6 | 3 |
| b | 3 | 2 | 1 |
| b | 7 | 3 | 5 |
Expected output:
| grp | sum(data1) | sum(data2)/sum(data1) | sum(data3)/sum(data1) |
|---|---|---|---|
| a | 6 | 1.166666667 | 0.83 |
| a | 10 | 0.5 | 0.6 |
Assume custom aggregation can be dependent on multiple columns and not always a simple division operation. I know using SQL query it's possible, but I am interested in an answer with apply and aggregate function if possible.