Skip to content

Instantly share code, notes, and snippets.

@schochastics
Created November 28, 2024 13:17
Show Gist options
  • Save schochastics/ddc83f8749a492457266878fb0c9e2a7 to your computer and use it in GitHub Desktop.
Save schochastics/ddc83f8749a492457266878fb0c9e2a7 to your computer and use it in GitHub Desktop.
left join data.frame list
library(dplyr)
library(data.table)
n <- 100
df <- data.frame(id=LETTERS[1:10],meta=letters[1:10])
other_dfs <- lapply(1:n,\(x) data.frame(id=LETTERS[1:10],values=rnorm(1:10)))
f <- function(n){
for(i in 1:n){
df <- left_join(df,other_dfs[[i]],by="id")
}
}
g <- function(){
df <- Reduce(function(x, y) left_join(x, y, by = "id"),
other_dfs,
init = df)
}
dt <- data.table(id=LETTERS[1:10],meta=letters[1:10])
other_dts <- lapply(1:n,\(x) data.table(id=LETTERS[1:10],values=rnorm(1:10)))
for (i in seq_along(other_dts)) {
setnames(other_dts[[i]], "values", paste0("values_", i))
}
h <- function(){
dt <- Reduce(function(x, y) merge(x, y, by = "id", all.x = TRUE), c(list(dt), other_dts))
}
microbenchmark::microbenchmark(
f(n),g(),h(),times = 10
)
@schochastics
Copy link
Author

Unit: milliseconds
 expr     min      lq    mean  median      uq     max neval
 f(n) 71.2929 71.9764 77.5014 76.5651 81.6266 87.9712    10
  g() 69.3343 73.1557 76.3624 74.2622 81.0695 87.9320    10
  h() 35.4880 36.0954 37.1046 36.6920 38.0468 40.1230    10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment