I like Reduce
in base R, so you could do:
Reduce("+", lapply(Reduce(c, ll), colSums))
# [1] 26 66 106 146
BENCHMARKING
set.seed(100)
l1 <- matrix(sample(100, 100000, replace = TRUE),ncol=1000)
l2 <- matrix(sample(100, 1000000, replace = TRUE),ncol=1000)
l <- list(l1,l2)
ll <- rep(list(l), 100)
str(ll)
# List of 100
# $ :List of 2
# ..$ : int [1:100, 1:1000] 31 26 56 6 47 49 82 38 55 18 ...
# ..$ : int [1:1000, 1:1000] 45 22 77 7 85 32 80 71 52 38 ...
# $ :List of 2
# ..$ : int [1:100, 1:1000] 31 26 56 6 47 49 82 38 55 18 ...
# ..$ : int [1:1000, 1:1000] 45 22 77 7 85 32 80 71 52 38 ...
# $ :List of 2
# ..$ : int [1:100, 1:1000] 31 26 56 6 47 49 82 38 55 18 ...
# ..$ : int [1:1000, 1:1000] 45 22 77 7 85 32 80 71 52 38 ...
# $ :List of 2
# ..$ : int [1:100, 1:1000] 31 26 56 6 47 49 82 38 55 18 ...
# ..$ : int [1:1000, 1:1000] 45 22 77 7 85 32 80 71 52 38 ...
library(purrr)
library(microbenchmark)
library(magrittr)
f_G.Grothendieck1 <- function(ll) rowSums(sapply(do.call(c, ll), colSums))
f_G.Grothendieck2 <- function(ll) colSums(do.call(rbind, do.call(c, ll)))
f_G.Grothendieck3 <- function(ll)
ll %>% do.call(what = c) %>% sapply(colSums) %>% rowSums
f_G.Grothendieck4 <- function(ll)
ll %>% do.call(what = c) %>% do.call(what = rbind) %>% colSums
f_d.b <- function(ll)
colSums(do.call(rbind, lapply(lapply(rapply(ll, enquote, how="unlist"), eval), colSums)))
f_alistaire1 <- function(ll) ll %>% flatten() %>% map(colSums) %>% reduce(`+`)
f_alistaire2 <- function(ll) ll %>% flatten() %>% invoke(rbind, .) %>% colSums()
f_989 <- function(ll) Reduce("+", lapply(Reduce(c, ll), colSums))
r <- f_G.Grothendieck1(ll)
# [1] TRUE
all(r == f_G.Grothendieck2(ll))
# [1] TRUE
all(r == f_G.Grothendieck3(ll))
# [1] TRUE
all(r == f_G.Grothendieck4(ll))
# [1] TRUE
all(r == f_d.b(ll))
# [1] TRUE
all(r == f_alistaire1(ll))
# [1] TRUE
all(r == f_alistaire2(ll))
# [1] TRUE
all(r == f_989(ll))
# [1] TRUE
res <- microbenchmark(
f_G.Grothendieck1(ll), f_G.Grothendieck2(ll), f_G.Grothendieck3(ll),
f_G.Grothendieck4(ll), f_d.b(ll), f_alistaire1(ll), f_alistaire2(ll), f_989(ll))
print(res, order="mean")
# Unit: milliseconds
# expr min lq mean median uq max neval
# f_989(ll) 84.67007 87.05084 87.50351 87.70766 88.25692 91.12715 100
# f_alistaire1(ll) 85.00209 87.35116 87.83935 87.91318 88.32242 98.69927 100
# f_d.b(ll) 85.15563 87.74943 88.01660 88.23258 88.72280 89.89943 100
# f_G.Grothendieck1(ll) 85.38729 87.77707 88.40864 88.45328 89.03604 100.78963 100
# f_G.Grothendieck3(ll) 85.85933 87.85805 88.69445 88.68118 89.28618 104.93881 100
# f_G.Grothendieck4(ll) 1150.27718 1200.80601 1205.76164 1206.48442 1211.72250 1310.64802 100
# f_alistaire2(ll) 1178.14509 1202.61153 1207.05208 1205.89009 1211.49820 1325.72315 100
# f_G.Grothendieck2(ll) 1177.02283 1204.55166 1210.40954 1208.95338 1213.82218 1278.82715 100
lapply(unlist(ll, recursive = FALSE), colSums)
ORlapply(lapply(rapply(ll, enquote, how="unlist"), eval), colSums)
- d.b