代码之家  ›  专栏  ›  技术社区  ›  MAPK

按一列右键联接多个数据帧

r
  •  0
  • MAPK  · 技术社区  · 6 年前

    我有这三个数据帧 df1 df2 , df3 .

    df1<- structure(list(ControlRate = structure(c(1L, 1L, 1L), .Label = c("24000", 
    "26000", "28000", "30000"), class = "factor"), Mean = c(92.914223793805, 
    125.810174859037, 152.350610715905)), row.names = c(NA, 3L), class = "data.frame")
    
    df2 <- structure(list(ControlRate = structure(c(1L, 1L, 1L), .Label = c("24000", 
    "26000", "28000", "30000"), class = "factor"), SD = c(19.7498590603068, 
    33.5137097117632, 40.4792131612947)), row.names = c(NA, 3L), class = "data.frame")
    
    df3 <- structure(list(ControlRate = structure(c(1L, 1L, 1L), .Label = c("24000", 
    "26000", "28000", "30000"), class = "factor"), Count = c(36L, 
    117L, 198L)), row.names = c(NA, 3L), class = "data.frame")
    

    而不是使用 cbind(df1,df2$SD, df3$Count) ,我想按如下方式使用merge,但它不起作用。我在这里做错什么了?

    Reduce(function(dtf1, dtf2) merge(dtf1, dtf2, by = "ControlRate", all.x = TRUE),
           list(df1, df2, df3))
    
    0 回复  |  直到 6 年前
        1
  •  3
  •   M--    6 年前
    library(tidyverse)
    
    list(df1,df2,df3) %>%
      map(rowid_to_column) %>%
      reduce(left_join, by = c("rowid","ControlRate")) %>% 
      select(-rowid)
    
    #>   ControlRate      Mean       SD Count
    #> 1       24000  92.91422 19.74986    36
    #> 2       24000 125.81017 33.51371   117
    #> 3       24000 152.35061 40.47921   198