rbind two data.frame preserving row order and row names

前端 未结 2 513
孤街浪徒
孤街浪徒 2021-02-08 15:14

I have a list of data.frame objects which i would like to row append to one another, ie merge(..., all=T). However, merge seems to remove

2条回答
  •  天命终不由人
    2021-02-08 15:23

    Since you know you are not actually merging, but just rbind-ing, maybe something like this will work. It makes use of rbind.fill from "plyr". To use it, specify a list of the data.frames you want to rbind.

    RBIND <- function(datalist) {
      require(plyr)
      temp <- rbind.fill(datalist)
      rownames(temp) <- unlist(lapply(datalist, row.names))
      temp
    }
    RBIND(list(x, y))
    #               a  b  c  d
    # row_1         1  2  3  4
    # another_row1  2  3  4  5
    # row_2        10 20 30 NA
    # another_row2 20 30 40 NA
    

提交回复
热议问题