Home > database >  How to append/combine more than 2 lists of data frames into one list efficiently?
How to append/combine more than 2 lists of data frames into one list efficiently?

Time:08-09

I have lists of data frames that I want to combine into one. For instance:

ls1 <- list(data.frame(A = rnorm(3), B = rnorm(3)), data.frame(A = rnorm(3), B = rnorm(3)))
ls2 <- list(data.frame(A = rnorm(3), B = rnorm(3)), data.frame(A = rnorm(3), B = rnorm(3)))
ls3 <- list(data.frame(A = rnorm(3), B = rnorm(3)), data.frame(A = rnorm(3), B = rnorm(3)))

I found this enter image description here

It creates a list of length 2 with sublists of length 6. My desired output is this:

res2 <- append(ls1, ls2)
res2 <- append(res2, ls3)
res2 |> View()

enter image description here

I have more than 10 lists of data frames that I want to combine into one. Instead of using append, is there a more efficient way to combine them all in one list?

CodePudding user response:

One option could be:

Reduce(c, mget(ls(pattern = "ls")))

Compared with the original results:

setequal(Reduce(c, mget(ls(pattern = "ls"))), res2)

[1] TRUE

CodePudding user response:

unlist will be a little faster than Reduce.

n <- 3L
for (i in 1:1e3) assign(paste0("ls", i), list(data.frame(A = rnorm(n), B = rnorm(n)), data.frame(A = rnorm(n), B = rnorm(n))))

f1 <- function(envir = .GlobalEnv) unlist(mget(ls(pattern = "ls", envir = envir), envir), FALSE, FALSE)
f2 <- function(envir = .GlobalEnv) Reduce(c, mget(ls(pattern = "ls", envir = envir), envir))

microbenchmark::microbenchmark(f1 = f1(),
                               f2 = f2(),
                               check = "identical")
#> Unit: milliseconds
#>  expr    min      lq     mean  median      uq     max neval
#>    f1 1.8792 1.91630 1.971708 1.93410 1.98085  3.7408   100
#>    f2 6.3691 6.47915 7.100086 6.59355 6.96825 15.7032   100
  • Related