I thought this was a very interesting ask, and FJCC's solution is excellent.
Below I made a more complex version of the ask, requiring the merge of 3 such lists, each longer, the idea being to give profiling more of a change to show performance differences.
FJCC's solution I called 'f2_lapply_docall', I wanted to compare with direct matrix building and filling out with a loop. They seem to be roughly on a par, if you have expectations whether you would be merging very many more lists at a time, and what typical length of lists would be you might get more relevant performance metrics than the ones here.
set.seed(42)
v1 <- sample.int(1000,1000,replace=TRUE)
v2 <- sample.int(1000,1000,replace=TRUE)
v3 <- sample.int(1000,1000,replace=TRUE)
names(v1) <- sort(paste0("x",formatC(sample.int(3000,1000,replace=FALSE),width=4,flag="0")))
names(v2) <- sort(paste0("x",formatC(sample.int(3000,1000,replace=FALSE),width=4,flag="0")))
names(v3) <- sort(paste0("x",formatC(sample.int(3000,1000,replace=FALSE),width=4,flag="0")))
results <- list(v1, v2,v3)
desired <- as.matrix(dplyr::bind_rows(results))
f1_dplyr <- function(r){
as.matrix(dplyr::bind_rows(r))
}
f2_lapply_docall <- function(r){
UL <- unlist(r)
NMs <- unique(names(UL))
RowFunc <- function(Vec){
tmp <- Vec[NMs]
names(tmp) <- NMs
return(tmp)
}
OutList <- lapply(r, RowFunc)
do.call(rbind, OutList)
}
f3_matrix_forloop <- function(r){
NMs <- unique(names(unlist(r)))
lengthr <- length(r)
newmat <- matrix(nrow=lengthr,
ncol = length(NMs),dimnames = list(NULL,
NMs))
for(row in seq_len(lengthr)){
newmat[row ,] <- r[[row]][NMs]
}
newmat
}
library(bench)
mark(f1_dplyr(results),
f2_lapply_docall(results),
f3_matrix_forloop(results),
time_unit = "ns",
min_time = 5)
# A tibble: 3 x 13
expression min median `itr/sec` mem_alloc `gc/sec` n_itr n_gc total_time result memory
<bch:expr> <dbl> <dbl> <dbl> <bch:byt> <dbl> <int> <dbl> <dbl> <list> <list>
1 f1_dplyr(results) 14217100. 17419950. 56.1 723KB 17.0 208 63 3706866100. <int[...]> <Rprofmem>
2 f2_lapply_docall(results) 455300. 560800. 1622. 586KB 18.4 7499 85 4623899600. <int[...]> <Rprofmem>
3 f3_matrix_forloop(results) 476100. 609000. 1569. 636KB 19.4 7209 89 4595938200.