强制转换为 matrix,然后转换为 rowSums。
rowSums(matrix(unlist(list_example), ncol=length(list_example)))
# [1] 3.452629 1.049781 4.562073
或者在rbind之后使用colSums。
colSums(do.call(rbind, list_example))
# [1] 3.452629 1.049781 4.562073
数据
为了清楚起见,我在这里使用了一个较小的列表。
set.seed(42)
list_example <- list()
for(i in 1:4){
list_example[[i]] <- rnorm(3,0,1)
}
set.seed(42)
list_example <- list()
for(i in seq(1e5)) {list_example[[i]] <- rnorm(3,0,1)}
microbenchmark::microbenchmark(list2DF=rowSums(list2DF(list_example)),
rowSums=rowSums(matrix(unlist(list_example), ncol=length(list_example))),
Reduce=Reduce(`+`, list_example),
data.table=sapply(data.table::transpose(list_example), sum),
data.table2=unlist(lapply(data.table::transpose(list_example), sum)),
data.table3=vapply(data.table::transpose(list_example), sum, 0),
colSums=colSums(do.call(rbind, list_example)),
times=100L)
# Unit: milliseconds
# expr min lq mean median uq max neval cld
# list2DF 736.073711 771.667592 803.028556 795.729308 830.898798 920.98592 100 d
# rowSums 7.848909 7.943765 9.436778 8.002277 8.085766 106.27099 100 a
# Reduce 60.616783 65.026398 70.332936 68.337665 71.649778 136.14140 100 b
# data.table 6.371089 6.508093 7.447124 6.671454 6.846889 73.26115 100 a
# data.table2 6.286547 6.433442 7.792743 6.578557 6.842724 96.99866 100 a
# data.table3 6.254795 6.447059 6.796695 6.597071 6.796004 13.22731 100 a
# colSums 60.533471 69.789563 89.329641 78.337311 95.657619 232.72070 100 c