The development version 1.10.5 of the data.table package (see here for installation instructions) has three new functions for calculating aggregates at various levels of groupings which can be used here.
Note that OP's expected result contains contiguous row numbers 1 to 15 which suggest that the OP is expecting one data.frame or data.table rather than a list as preferred by Frank. However, we will show below that also a data.table can be printed in an eye-friendly way.
rollup()
With the new rollup() function and ordering by Reg
library(data.table) # development version 1.10.5 as of 2015-09-10
setDT(df)
rollup(df, j = list(Pop = sum(Pop)), by = c("Reg", "Res"))[order(Reg)]
we do get
Reg Res Pop
1: A Urban 500414
2: A Rural 500501
3: A NA 1000915
4: B Urban 499922
5: B Rural 500016
6: B NA 999938
7: C Urban 501638
8: C Rural 499274
9: C NA 1000912
10: D Urban 499804
11: D Rural 499825
12: D NA 999629
13: E Urban 499917
14: E Rural 500386
15: E NA 1000303
16: NA NA 5001697
The respective totals are indicated by NA (including a grand total). If we want to better reproduce the expected result, the grand total can be removed and NA be replaced by Total:
rollup(df, j = list(Pop = sum(Pop)), by = c("Reg", "Res"))[order(Reg)][
is.na(Res), Res := "Total"][!is.na(Reg)]
Reg Res Pop
1: A Urban 500414
2: A Rural 500501
3: A Total 1000915
4: B Urban 499922
5: B Rural 500016
6: B Total 999938
7: C Urban 501638
8: C Rural 499274
9: C Total 1000912
10: D Urban 499804
11: D Rural 499825
12: D Total 999629
13: E Urban 499917
14: E Rural 500386
15: E Total 1000303
Note that the Total rows appear below the details rows which isn't fully in line with OP's expected result.
groupingsets()
With the groupingsets() function, the aggregations can be controlled in great detail:
groupingsets(df, j = list(Pop = sum(Pop)), by = c("Reg", "Res"),
sets = list("Reg", c("Reg", "Res")))[order(Reg)][
is.na(Res), Res := "Total"][]
Reg Res Pop
1: A Total 1000915
2: A Urban 500414
3: A Rural 500501
4: B Total 999938
5: B Urban 499922
6: B Rural 500016
7: C Total 1000912
8: C Urban 501638
9: C Rural 499274
10: D Total 999629
11: D Urban 499804
12: D Rural 499825
13: E Total 1000303
14: E Urban 499917
15: E Rural 500386
Now, the Total rows appear above the details rows and no grand total was created at all.
Nicely printed "classic" data.table solutions
Up to now, two "classic" data.table solutions were posted by Psidom and Hack-R.
Both could be re-written more concisely as
rbind(df[, .(Res = "Total", Pop = sum(Pop)), by = Reg], df)[order(Reg)]
The result can be printed in an "eye-friendly" way with blank lines between the groups using
rbind(df[, .(Res = "Total", Pop = sum(Pop)), by = Reg], df)[
order(Reg), {print(data.table(Reg, .SD), row.names = FALSE); cat("\n")}, by = Reg]
Reg Res Pop
A Total 1000915
A Urban 500414
A Rural 500501
Reg Res Pop
B Total 999938
B Urban 499922
B Rural 500016
Reg Res Pop
C Total 1000912
C Urban 501638
C Rural 499274
Reg Res Pop
D Total 999629
D Urban 499804
D Rural 499825
Reg Res Pop
E Total 1000303
E Urban 499917
E Rural 500386