Created
November 28, 2024 13:17
-
-
Save schochastics/ddc83f8749a492457266878fb0c9e2a7 to your computer and use it in GitHub Desktop.
left join data.frame list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
) |
Author
schochastics
commented
Nov 28, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment