同时在列表中合并多个data.frames [英] Simultaneously merge multiple data.frames in a list
问题描述
var1
和var2
在下面的代码中)。如果data.frames在列中是相同的,那么我只能使用 rbind
,plyr的 rbind.fill 将完成这项工作,但这些数据并非如此。 因为合并
命令只适用于2个数据框架,所以我转向互联网寻求想法。我从这里获得了这个,这在R 2.7中完美无缺。 2,这是我当时所拥有的:
merge.rec< - function(.list,...) {
if(length(.list)== 1)return(.list [[1]])
Recall(c(list(merge(.list [[1]]) 2]],...)),.list [ - (1:2)]),...)
}
我会像这样调用函数:
df< - merge.rec (my.list,by.x = c(var1,var2),
by.y = c(var1,var2),all = T,suffixes = c( ))
但在2.7.2之后的任何R版本中,包括2.11和2.12,这段代码失败,出现以下错误:
match.names中的错误(clabs,names(xi)):
名称不符合以前的名字
(偶然地,我看到其他参考这个错误其他地方没有分辨率)。
有没有办法解决这个问题?
另一个问题具体问题如何在R中使用dplyr执行多个左连接。这个问题被标记为这个副本,所以我在这里回答:
library(dplyr)
x< - data_frame(i = c(a,b,c),j = 1:3)
y < - data_frame(i = c(b,c,d ,k = 4:6)
z< - data_frame(i = c(c,d,a),l = 7:9)
list(x,y,z )%>%
Reduce(function(dtf1,dtf2)left_join(dtf1,dtf2,by =i),。)
#ijkl
#1 a 1 NA 9
#2 b 2 4 NA
#3 c 3 5 7
您还可以执行full_join()和inner_join()
列表(x,y,z)%>%
Reduce(function(dtf1,dtf2)full_join(dtf1,dtf2,by =i),。)
#Source:本地数据帧[4 x 4]
#ijkl
#1 a 1 NA 9
#2 b 2 4 NA
#3 c 3 5 7
#4 d NA 6 8
list(x,y,z)%>%
Reduce(function(dtf1,dtf2)inner_join(dtf1,dtf2,by =i),$)
#Source:框架[1 x 4]
#ijkl
#1 c 3 5 7
对于t他的完整性,这里是完整连接的基础R版本
Reduce(function(dtf1,dtf2)merge)(dtf1 ,dtf2,by =i,all = TRUE),
list(x,y,z))
#ijkl
#1 a 1 NA 9
#2 b 2 4 NA
#3 c 3 5 7
#4 d NA 6 8
I have a list of many data.frames that I want to merge. The issue here is that each data.frame differs in terms of the number of rows and columns, but they all share the key variables (which I've called "var1"
and "var2"
in the code below). If the data.frames were identical in terms of columns, I could merely rbind
, for which plyr's rbind.fill would do the job, but that's not the case with these data.
Because the merge
command only works on 2 data.frames, I turned to the Internet for ideas. I got this one from here, which worked perfectly in R 2.7.2, which is what I had at the time:
merge.rec <- function(.list, ...){
if(length(.list)==1) return(.list[[1]])
Recall(c(list(merge(.list[[1]], .list[[2]], ...)), .list[-(1:2)]), ...)
}
And I would call the function like so:
df <- merge.rec(my.list, by.x = c("var1", "var2"),
by.y = c("var1", "var2"), all = T, suffixes=c("", ""))
But in any R version after 2.7.2, including 2.11 and 2.12, this code fails with the following error:
Error in match.names(clabs, names(xi)) :
names do not match previous names
(Incidently, I see other references to this error elsewhere with no resolution).
Is there any way to solve this?
Another question asked specifically how to perform multiple left joins using dplyr in R . The question was marked as a duplicate of this one so I answer here:
library(dplyr)
x <- data_frame(i = c("a","b","c"), j = 1:3)
y <- data_frame(i = c("b","c","d"), k = 4:6)
z <- data_frame(i = c("c","d","a"), l = 7:9)
list(x,y,z) %>%
Reduce(function(dtf1,dtf2) left_join(dtf1,dtf2,by="i"), .)
# i j k l
#1 a 1 NA 9
#2 b 2 4 NA
#3 c 3 5 7
You can also perform full_join() and inner_join()
list(x,y,z) %>%
Reduce(function(dtf1,dtf2) full_join(dtf1,dtf2,by="i"), .)
#Source: local data frame [4 x 4]
# i j k l
#1 a 1 NA 9
#2 b 2 4 NA
#3 c 3 5 7
#4 d NA 6 8
list(x,y,z) %>%
Reduce(function(dtf1,dtf2) inner_join(dtf1,dtf2,by="i"), .)
#Source: local data frame [1 x 4]
# i j k l
#1 c 3 5 7
For the sake of completeness, here is a base R version of the full join
Reduce(function(dtf1, dtf2) merge(dtf1, dtf2, by = "i", all = TRUE),
list(x,y,z))
# i j k l
#1 a 1 NA 9
#2 b 2 4 NA
#3 c 3 5 7
#4 d NA 6 8
这篇关于同时在列表中合并多个data.frames的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!