我要合并7个不同的数据框。当我使用如下所示的基本合并功能时,出现错误:
new <- list(A, B, C, D, E, F, G) %>% Reduce(function(df1, df2, df3, df4, dtf5, df6, df7) left_join(df1,df2,by="ID"), .)
Error: cannot allocate vector of size 9.9 Gb
因此,我想通过在每个合并中选择几个列来解决此问题。数据集看起来像这样,但具有更多的列和行。
A B C D E F G
ID C1 C2 ID C3 ID C4 ID C5 ID C6 ID C7 C8 ID C9
1L 5 7 1L 3 2L 4 1L 10 2L 4 1L 5 9 1L 4
2L 9 3 2L 4 3L 7 2L 4 2L 0 10 2L 9
3L 0
一旦合并:
new
ID C1 C2 C3 C4 C5 C6 C7 C8 C9
1L 5 7 3 10 5 9 4
2L 9 3 4 4 4 4 0 10 9
3L 7 0
我尝试过的是这样的:
ncombined <- merge(x = A, y = B[,c("C3")], by = "ID", all.x = TRUE)
Reduce(function(dtf1, dtf2) merge(dtf1, dtf2, by = "i", all.x = TRUE),
list(A[,c("C1",
"C2")],B[,c("C3")],C[,c("C4")],D[,c("C5")],E[,c("C6")],F[,c("C7",
"C8")],G[,c("C9")]))
(摘自示例:Simultaneously merge multiple data.frames in a list merge only one or two columns from a different dataframe in R)
答案 0 :(得分:1)
可能不是最节省内存的方式,但是您可以尝试:
library(data.table)
data <- list(df1, df2, df3, df4, df5, df6, df7)
lapply(data, setDT)
for (df in data[-1]) df1 <- merge(df1, df, by = "ID", all = TRUE)
这应该将所有数据帧与df1连接起来。