Merge data frames based on rownames in R

后端 未结 1 445
庸人自扰
庸人自扰 2020-11-30 21:07

How can I merge the columns of two data frames, containing a distinct set of columns but some rows with the same names? The fields for rows that don\'t occur in bot

相关标签:
1条回答
  • 2020-11-30 22:07

    See ?merge:

    the name "row.names" or the number 0 specifies the row names.

    Example:

    R> de <- merge(d, e, by=0, all=TRUE)  # merge by row names (by=0 or by="row.names")
    R> de[is.na(de)] <- 0                 # replace NA values
    R> de
      Row.names   a   b   c   d   e   f   g   h   i  j  k  l  m  n  o  p  q  r  s
    1         1 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10 11 12 13 14 15 16 17 18 19
    2         2 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9  1  0  0  0  0  0  0  0  0  0
    3         3 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0  0 21 22 23 24 25 26 27 28 29
       t
    1 20
    2  0
    3 30
    
    0 讨论(0)
提交回复
热议问题