lrony* 2011-11-11 08:16 采纳率: 0%
浏览 411
已采纳

同时合并列表中的多个数据帧

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?

转载于:https://stackoverflow.com/questions/8091303/simultaneously-merge-multiple-data-frames-in-a-list

  • 写回答

6条回答 默认 最新

  • 关注

    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, using the 3 sample data frames below:

    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)
    

    Update June 2018: I divided the answer in three sections representing three different ways to perform the merge. You probably want to use the purrr way if you are already using the tidyverse packages. For comparison purposes below, you'll find a base R version using the same sample dataset.

    Join them with reduce from the purrr package

    The purrr package provides a reduce function which has a concise syntax:

    library(tidyverse)
    list(x, y, z) %>% reduce(left_join, by = "i")
    #  A tibble: 3 x 4
    #  i       j     k     l
    #  <chr> <int> <int> <int>
    # 1 a      1    NA     9
    # 2 b      2     4    NA
    # 3 c      3     5     7
    

    You can also perform other joins, such as a full_join or inner_join:

    list(x, y, z) %>% reduce(full_join, by = "i")
    # A tibble: 4 x 4
    # i       j     k     l
    # <chr> <int> <int> <int>
    # 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(inner_join, by = "i")
    # A tibble: 1 x 4
    # i       j     k     l
    # <chr> <int> <int> <int>
    # 1 c     3     5     7
    

    dplyr::left_join() with base R Reduce()

    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
    

    Base R merge() with base R Reduce()

    And for comparison purposes, here is a base R version of the left join

     Reduce(function(dtf1, dtf2) merge(dtf1, dtf2, by = "i", all.x = TRUE),
            list(x,y,z))
    #   i j  k  l
    # 1 a 1 NA  9
    # 2 b 2  4 NA
    # 3 c 3  5  7
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 Marscode IDE 如何预览新建的 HTML 文件
  • ¥15 K8S部署二进制集群过程中calico一直报错
  • ¥15 java python或者任何一种编程语言复刻一个网页
  • ¥20 如何通过代码传输视频到亚马逊平台
  • ¥15 php查询mysql数据库并显示至下拉列表中
  • ¥15 freertos下使用外部中断失效
  • ¥15 输入的char字符转为int类型,不是对应的ascall码,如何才能使之转换为对应ascall码?或者使输入的char字符可以正常与其他字符比较?
  • ¥15 devserver配置完 启动服务 无法访问static上的资源
  • ¥15 解决websocket跟c#客户端通信
  • ¥30 Python调用dll文件输出Nan重置dll状态