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条)

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?