I have a two-parter question. In creating a dataset similar to what I have, we can start with the airquality dataset:
myaqm <- melt(airquality, id=c("Month", "Day"), na.rm = TRUE)
myaqm_dcast <- dcast(myaqm, Day+variable~Month,
value.var = "value", sum, margins=c("Day", "variable", "Month"))
Giving us this:
> head(myaqm_dcast, n=10)
Day variable 5 6 7 8 9 (all)
1 1 Ozone 41.0 0.0 135.0 39.0 96.0 311.0
2 1 Solar.R 190.0 286.0 269.0 83.0 167.0 995.0
3 1 Wind 7.4 8.6 4.1 6.9 6.9 33.9
4 1 Temp 67.0 78.0 84.0 81.0 91.0 401.0
5 1 (all) 305.4 372.6 492.1 209.9 360.9 1740.9
6 2 Ozone 36.0 0.0 49.0 9.0 78.0 172.0
7 2 Solar.R 118.0 287.0 248.0 24.0 197.0 874.0
8 2 Wind 8.0 9.7 9.2 13.8 5.1 45.8
9 2 Temp 72.0 74.0 85.0 81.0 92.0 404.0
10 2 (all) 234.0 370.7 391.2 127.8 372.1 1495.8
First question: How can you remove Wind from each daily (all) sub-total?
I'm sure we're meant to use dplyr
, perhaps wholly instead of dcast
, however for the life of me I can't find an example which demonstrates sub-total exclusions.
Second question: Similarly we're also trying to work out how to include a daily average row of all variables, excluding Temp. So the dataset would look something like this:
Day variable 5 6 7 8 9 (all)
1 1 Ozone 41.0 0.0 135.0 39.0 96.0 311.0
2 1 Solar.R 190.0 286.0 269.0 83.0 167.0 995.0
3 1 Wind 7.4 8.6 4.1 6.9 6.9 33.9
4 1 Temp 67.0 78.0 84.0 81.0 91.0 401.0
5 1 Day Avg 79.5 98.2 136.0 43.0 90.0 446.6
6 1 (all) 298.0 364.0 488.0 203.0 354.0 1707.0
7 2 Ozone 36.0 0.0 49.0 9.0 78.0 172.0
8 2 Solar.R 118.0 287.0 248.0 24.0 197.0 874.0
9 2 Wind 8.0 9.7 9.2 13.8 5.1 45.8
10 2 Temp 72.0 74.0 85.0 81.0 92.0 404.0
11 2 Day Avg 54.0 98.9 102.1 15.6 93.4 363.9
12 2 (all) 226.0 361.0 382.0 114.0 367.0 1450.0
Here, Day Avg is the mean of Ozone, Solar.R and Wind -- does not include Temp.
And (all) is the sum of Ozone, Solar.R and Temp -- does not include Wind.
I think it's another case for dplyr
, and it would be a similar solution, but I thought I'd ask both questions.
Edit: It's worth mentioning that my real-world data has much more than four variables each 'day', some of which don't actually appear each day, but nevertheless need to be included in the daily 'Day Avg' and '(all)' calculations, so it would be cleaner to exclude variables by name (ie. Temp or Wind) rather than naming all the variables than need to be included (ie. Ozone and Solar.R).