The fastest and most efficient way that I know is using the data.table::transpose
function (if the length of your vector is low-dimensional):
as.data.frame(data.table::transpose(vectorList), col.names = names(vectorList[[1]]))
However, you will need to set the column names manually as data.table::transpose
removes them. There is also a purrr::transpose
function that does not remove the column names but it seems to be slower.
Below a small benchmark including the suggestions of the other users:
vectorList = lapply(1:1000, function(i) (c("number" = i, "square root" = sqrt(i))))
bench = microbenchmark::microbenchmark(
dplyr = dplyr::bind_rows(lapply(vectorList, as.data.frame.list)),
rbindlist = data.table::rbindlist(lapply(vectorList, as.data.frame.list)),
Reduce = Reduce(rbind, vectorList),
transpose_datatable = as.data.frame(data.table::transpose(vectorList), col.names = names(vectorList[[1]])),
transpose_purrr = data.table::as.data.table(purrr::transpose(vectorList)),
do.call = as.data.frame(do.call(rbind, vectorList)),
times = 10)
bench
# Unit: microseconds
# expr min lq mean median uq max neval cld
# dplyr 286963.036 292850.136 320345.1137 310159.7380 341654.619 385399.851 10 b
# rbindlist 285830.750 289935.336 306120.7257 309581.1895 318131.031 324217.413 10 b
# Reduce 8573.474 9073.649 12114.5559 9632.1120 11153.511 33446.353 10 a
# transpose_datatable 372.572 424.165 500.8845 479.4990 532.076 701.822 10 a
# transpose_purrr 539.953 590.365 672.9531 671.1025 718.757 911.343 10 a
# do.call 452.915 537.591 562.9144 570.0825 592.334 641.958 10 a
# now use bigger list and disregard the slowest
vectorList = lapply(1:100000, function(i) (c("number" = i, "square root" = sqrt(i))))
bench.big = microbenchmark::microbenchmark(
transpose_datatable = as.data.frame(data.table::transpose(vectorList), col.names = names(vectorList[[1]])),
transpose_purrr = data.table::as.data.table(purrr::transpose(vectorList)),
do.call = as.data.frame(do.call(rbind, vectorList)),
times = 10)
bench.big
# Unit: milliseconds
# expr min lq mean median uq max neval cld
# transpose_datatable 3.470901 4.59531 4.551515 4.708932 4.873755 4.91235 10 a
# transpose_purrr 61.007574 62.06936 68.634732 65.949067 67.477948 97.39748 10 b
# do.call 97.680252 102.04674 115.669540 104.983596 138.193644 151.30886 10 c