cbind 2 dataframes with different number of rows

前端 未结 3 1164
陌清茗
陌清茗 2020-11-30 10:25

I have two lists named h and g. They each contain 244 dataframes and they look like the following:

h[[1]]
   year  avg    hr   sal
         


        
3条回答
  •  天命终不由人
    2020-11-30 10:56

    We can use Map with cbind.fill (from rowr) to cbind the corresponding 'data.frame' from 'h' and 'g'.

    library(rowr)
    Map(cbind.fill, h, g, MoreArgs = list(fill=NA))
    

    Update

    Based on the expected output showed, it seems like the OP wanted a merge instead of cbind

    f1 <- function(...) merge(..., all = TRUE, by = 'year')
    Map(f1, h, g)
    #[[1]]
    #  year   avg hr  sal  pos   fld
    #1 2010 0.300 31 2000    A 0.990
    #2 2011 0.290 30 4000    B 0.995
    #3 2012 0.275 14  600     NA
    #4 2013 0.280 24  800    C 0.970
    #5 2014 0.295 18 1000    B 0.980
    #6 2015 0.330 26 7000    D 0.990
    #7 2016 0.315 40 9000     NA
    

    Or as @Colonel Beauvel mentioned, this can be made compact

    Map(merge, h, g, by='year', all=TRUE)
    

    data

    h <- list(structure(list(year = 2010:2016, avg = c(0.3, 0.29, 0.275, 
    0.28, 0.295, 0.33, 0.315), hr = c(31L, 30L, 14L, 24L, 18L, 26L, 
     40L), sal = c(2000L, 4000L, 600L, 800L, 1000L, 7000L, 9000L)), .Names = c("year", 
     "avg", "hr", "sal"), class = "data.frame", row.names = c("1", 
     "2", "3", "4", "5", "6", "7")))
    
    g <- list(structure(list(year = c(2010L, 2011L, 2013L, 2014L, 2015L
    ), pos = c("A", "B", "C", "B", "D"), fld = c(0.99, 0.995, 0.97, 
    0.98, 0.99)), .Names = c("year", "pos", "fld"), class = "data.frame",
    row.names = c("1", 
    "2", "3", "4", "5")))
    

提交回复
热议问题