Generate data

generate_data <- function(n_list,
                          n_columns,
                          n_rows) {
  exe <- function(n_columns = n_columns,
                  n_rows = n_rows) {
    as.data.frame(
      replicate(
        n = n_columns,
        expr = rnorm(n = n_rows)
      )
    )
  }
  n_columns <- rep(
    x = n_columns,
    times = n_list
  )
  lapply(
    X = n_columns,
    FUN = exe,
    n_rows = n_rows
  )
}
list_of_dataframes <- generate_data(
  n_list = 5,
  n_columns = 5,
  n_rows = 5
)

Base R

do.call(
  what = "rbind",
  args = list_of_dataframes
)

dplyr

dplyr::bind_rows(
  list_of_dataframes
)
# adding the .id argument creates a new column
# with labels for each list
# dplyr::bind_rows(
#   list_of_dataframes,
#   .id = "column_label"
# )

data.table

data.table::rbindlist(
  l = list_of_dataframes
)

Benchmark

microbenchmark::microbenchmark(
  base = do.call(
    what = "rbind",
    args = list_of_dataframes
  ),
  dplyr = dplyr::bind_rows(
    list_of_dataframes,
    .id = "column_label"
  ),
  data.table = data.table::rbindlist(
    l = list_of_dataframes
  ),
  times = 1000
)


jeksterslabds/jeksterslabRnotes documentation built on July 17, 2020, 3:12 p.m.