From the table below, I would like to create two columns that aggregate 'amount' depending on the value of 'number' and 'type'.
number | type | amount |
---|---|---|
1 | A | 10 |
1 | A | 20 |
2 | A | 10 |
3 | B | 20 |
2 | B | 10 |
1 | B | 20 |
Here's the table I would like to get. The first column I want to create is 'amount A', which is the aggregation of the rows with 'A' in 'type' grouped by 'number'. The other one 'amount A+B' is the aggregation of all the rows grouped by 'number' regardless the value of 'type'.
number | amount A | amount A+B |
---|---|---|
1 | 30 | 50 |
2 | 10 | 20 |
3 | 0 | 20 |
I only came up with the way to create subsets and create two columns separately. But I wonder if there is more efficient way.