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 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型