How does one merge dataframes by row name without adding a “Row.names” column?

前端 未结 2 1667
慢半拍i
慢半拍i 2020-12-30 04:28

If I have two data frames, such as:

df1 = data.frame(x=1:3,y=1:3,row.names=c(\'r1\',\'r2\',\'r3\'))
df2 = data.frame(z=5:7,row.names=c(\'r5\',\'r6\',\'r7\'))         


        
相关标签:
2条回答
  • 2020-12-30 04:32

    From the help of merge:

    If the matching involved row names, an extra character column called Row.names is added at the left, and in all cases the result has ‘automatic’ row names.

    So it is clear that you can't avoid the Row.names column at least using merge. But maybe to remove this column you can subset by name and not by index. For example:

    dd <- merge(df1,df2,by=0,all=TRUE) ## by=0 easier to write than row.names , 
                                       ## TRUE is cleaner than T
    

    Then I use row.names to subset like this :

    res <- subset(dd,select=-c(Row.names))
    rownames(res) <- dd[,'Row.names']
      x  y  z
    1  1  1 NA
    2  2  2 NA
    3  3  3 NA
    4 NA NA  5
    5 NA NA  6
    6 NA NA  7
    
    0 讨论(0)
  • 2020-12-30 04:35

    Not sure if it's any easier to remember, but you can do it all in one step using transform.

    transform(merge(df1,df2,by=0,all=TRUE), row.names=Row.names, Row.names=NULL)
    #    x  y  z
    #r1  1  1 NA
    #r2  2  2 NA
    #r3  3  3 NA
    #r5 NA NA  5
    #r6 NA NA  6
    #r7 NA NA  7
    
    0 讨论(0)
提交回复
热议问题