Is this what you mean by adding a total row?
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
DF1 <- data.frame(PROVINCE = rep(c("A", "B", "C"), 4),
ETHNICITY = rep(c("Y", "Z"), 6),
WEIGHT = runif(12, min = 40, max = 60))
DF2 <- data.frame(PROVINCE = rep(c("A", "B", "C"), 4),
ETHNICITY = rep(c("Y", "Z"), 6),
WEIGHT = runif(12, min = 40, max = 60))
MyList <- list(DF1, DF2)
MyFunc <- function(DatFrame) {
tmp <- reshape2::dcast(data = DatFrame, formula = PROVINCE ~ ETHNICITY,
value.var = "WEIGHT", fun.aggregate = sum)
SUMS <- tmp %>% summarize(across(.col=where(is.numeric), sum))
SummaryRow <- bind_cols(data.frame(PROVINCE="TOTAL"),SUMS)
bind_rows(tmp, SummaryRow)
}
DCAST_out <- lapply(X = MyList, FUN = MyFunc)
DCAST_out
#> [[1]]
#> PROVINCE Y Z
#> 1 A 108.14376 109.16078
#> 2 B 97.54265 114.88119
#> 3 C 91.41058 98.30363
#> 4 TOTAL 297.09699 322.34560
#>
#> [[2]]
#> PROVINCE Y Z
#> 1 A 100.91991 94.33169
#> 2 B 106.15745 114.00632
#> 3 C 98.59599 103.73327
#> 4 TOTAL 305.67335 312.07127
Created on 2020-12-13 by the reprex package (v0.3.0)