rbind dataframes with a different column name

前端 未结 5 513
伪装坚强ぢ
伪装坚强ぢ 2020-11-27 20:28

I\'ve 12 data frames, each one contains 6 columns: 5 have the same name, 1 is different. Then when I call rbind() I get:

Error in match.names(cla         


        
5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-27 20:44

    Another base R approach if you have data.frames with different column names:

    # Create a list of data frames
    df_list <- list()
    df_list[[1]] <- data.frame(x = 1, y = paste0("y1", 1:3))
    df_list[[2]] <- data.frame(x = 2, y = paste0("y2", 1:4))
    df_list[[3]] <- data.frame(x = 3, y = paste0("y3", 1:5), z = "z3")
    df_list
    #> [[1]]
    #>   x   y
    #> 1 1 y11
    #> 2 1 y12
    #> 3 1 y13
    #> 
    #> [[2]]
    #>   x   y
    #> 1 2 y21
    #> 2 2 y22
    #> 3 2 y23
    #> 4 2 y24
    #> 
    #> [[3]]
    #>   x   y  z
    #> 1 3 y31 z3
    #> 2 3 y32 z3
    #> 3 3 y33 z3
    #> 4 3 y34 z3
    #> 5 3 y35 z3
    
    # This works when the column names are the same
    do.call(rbind, df_list[1:2])
    #>   x   y
    #> 1 1 y11
    #> 2 1 y12
    #> 3 1 y13
    #> 4 2 y21
    #> 5 2 y22
    #> 6 2 y23
    #> 7 2 y24
    
    # but fails when the column names differ
    do.call(rbind, df_list)
    #> Error in rbind(deparse.level, ...): numbers of columns of arguments do not match
    
    # This can fill the unmatched columns with NA's without 
    # depending on other packages:
    Reduce(rbind, Map(function(x) {
      x[, setdiff(unique(unlist(lapply(df_list, colnames))), names(x))] <- NA; 
      return(x)
      }, 
      df_list))
    #>    x   y    z
    #> 1  1 y11 
    #> 2  1 y12 
    #> 3  1 y13 
    #> 4  2 y21 
    #> 5  2 y22 
    #> 6  2 y23 
    #> 7  2 y24 
    #> 8  3 y31   z3
    #> 9  3 y32   z3
    #> 10 3 y33   z3
    #> 11 3 y34   z3
    #> 12 3 y35   z3
    

提交回复
热议问题