[R] Creating dataframe with subtotals by all fields and totals of subtotals
Peter Lomas
peter.br.lomas at gmail.com
Wed Sep 14 21:33:34 CEST 2016
Hello R-Helpers,
I'm trying to to create a subtotal category for each column in a
dataset, and totals of subtotals, resulting in one data frame. I
figure I could do this by a whack of aggregate() and rbind(), but I'm
hoping there is a simpler way.
Below is a sample dataset. Underneath I create an "All" salesmen
subtotal and rbind it with the original dataset. I could do that for
"Drink" and "Region", then also do combinations of salesmen, drink,
and region subtotals. However, I'm hoping somebody out there is more
clever than I am.
Thanks!
Peter
dat <- structure(list(Date = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L,
2L, 3L, 1L, 2L, 3L, 1L, 2L,
3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L,
3L, 1L, 2L, 3L, 1L, 2L, 3L,
1L, 2L, 3L, 1L, 2L, 3L), .Label = c("2012-01",
"2012-02", "2012-03"), class =
"factor"), Region = structure(c(1L,
1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 1L,
2L, 2L, 2L, 3L, 3L,
3L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 1L,
1L, 1L, 2L, 2L, 2L,
3L, 3L, 3L), .Label = c("Zone1", "Zone2",
"Zone3"), class = "factor"),
Drink = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L), .Label = c("Cola",
"Orange Juice"), class = "factor"),
Salesman = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L,
2L, 2L, 2L, 2L, 2L), .Label = c("Joe", "Marty"), class
= "factor"),
Sales = c(10L, 36L, 9L, 39L, 12L, 61L, 62L, 28L, 82L, 1L,
38L, 14L, 55L, 50L, 62L, 64L, 69L,
65L, 28L, 85L, 66L, 66L,
75L, 59L, 31L, 14L, 93L, 35L, 24L,
11L, 4L, 30L, 2L, 17L,
36L, 47L)), .Names = c("Date",
"Region", "Drink", "Salesman",
"Sales"), class
= "data.frame", row.names = c(NA, -36L))
all.salesman <- aggregate(Sales~Date+Region+Drink, data=dat, FUN=sum)
all.salesman$Salesman <- "All"
dat <- rbind(dat, all.salesman)
More information about the R-help
mailing list