rbind two data.frame preserving row order and row names

前端 未结 2 475
孤街浪徒
孤街浪徒 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:36

    One way is to use row.names in merge so that you get it as an additional column.

    > merge(x, y, by=c("row.names", "a","b","c"), all.x=T, all.y=T, sort=F)
    
    #      Row.names  a  b  c  d
    # 1        row_1  1  2  3  4
    # 2 another_row1  2  3  4  5
    # 3        row_2 10 20 30 NA
    # 4 another_row2 20 30 40 NA
    

    Edit: By looking at the merge function with getS3method('merge', 'data.frame'), the row.names are clearly set to NULL (it is a pretty long code, so I won't paste here).

    # Commenting 
    # Lines 63 and 64
    row.names(x) <- NULL
    row.names(y) <- NULL
    
    # and 
    # Line 141 (thanks Ananda for pointing out)
    attr(res, "row.names") <- .set_row_names(nrow(res))
    

    and creating a new function, say, MERGE, works as the OP intends for this example. Just an experimentation.

提交回复
热议问题