I need to aggregate data in R. I have 8 columns, 3 of which are categorical and 5 of which are numeric and need to be summed conditionally based off of a combination of conditions from 2 of the categorical variables. My data looks like the below:
df <- structure(list(Color = c("Red", "Blue", "Blue", "Red", "Yellow"
), Weekend = c(1L, 0L, 1L, 0L, 1L), LeapYear = c(1L, 1L, 0L,
0L, 0L), Length = c(15L, 20L, 10L, 15L, 15L), Height = c(50L,
70L, 35L, 28L, 80L), Weight = c(120L, 130L, 120L, 105L, 140L),
Cost = c(25L, 50L, 55L, 65L, 80L), Purchases = c(5L, 10L,
5L, 10L, 15L)), class = "data.frame", row.names = c(NA, -5L
))
> df
Color Weekend LeapYear Length Height Weight Cost Purchases
1 Red 1 1 15 50 120 25 5
2 Blue 0 1 20 70 130 50 10
3 Blue 1 0 10 35 120 55 5
4 Red 0 0 15 28 105 65 10
5 Yellow 1 0 15 80 140 80 15
I want to aggregate this table with conditional summations,
for example, sum Length and Height, but only for Leap Years, sum Height and Cost, but only for Leap Years and Weekends.
And I want these conditional summations grouped by color to look like the below:
| Color | Length | Height | Weight | Cost | Purchases | Length_LeapYear | Height_LeapYear | Height_LeapYear_Weekend | Cost_LeapYear_Weekend | Purchases_Weekend |
|---|---|---|---|---|---|---|---|---|---|---|
| Red | 30 | 78 | 225 | 90 | 15 | 15 | 50 | 50 | 25 | 5 |
| Blue | 30 | 105 | 250 | 105 | 15 | 20 | 70 | 0 | 0 | 5 |
| Yellow | 15 | 80 | 140 | 80 | 15 | 0 | 0 | 0 | 0 | 15 |
I am working in dplyr and have the following working to sum multiple fields on the same condition using summarise_at():
df %>%
group_by(Color, Weekend, LeapYear) %>%
summarise_at(c(Length_LeapYear == "Length", Height_LeapYear == "Height"), ~sum(.[LeapYear==1]))
But when I try to add conditions for my remaining conditionally summed variables, this removes my prior summarizations. Here is my idea for how I imagine the code to work.
df %>%
group_by(Color, Weekend, LeapYear) %>%
summarise_at(c("Length", "Height", "Weight", "Cost", "Purchases"), sum) %>%
summarise_at(c(Length_LeapYear == "Length", Height_LeapYear == "Height"), ~sum(.[LeapYear==1])) %>%
summarise_at(c(Height_LeapYear_Weekend == "Height", Cost_LeapYear_Weekend == "Cost"), ~sum(.[LeapYear==1 & Weekend ==1])) %>%
summarise(Purchases_Weekend = sum(Purchases)) %>%
group_by(Color)
Ultimately, I feel like there must be a way to get each of these differently conditioned summations into one call of summarise_at(). I also am unsure of the best practice for summing conditionally on columns (Weekend and LeapYear) an then omitting those columns from the final table. So help on that would be appreciated as well.
For the record, I do know that I can perform these manipulations with one long call to summarise(), where I individually condition each derived column. However, in practice, my dataset is a lot wider than this, and it just makes more sense to try to condense the data manipulation by grouping like conditions.