合并后行名称不匹配

时间:2017-10-13 08:37:39

标签: r merge

我有3个简单的数据框。

我正试图通过'row.names'将它们全部合并到这里:Merging more than 2 dataframes in R by rownames

MyMerge <- function(x, y){
  df <- merge(x, y, by= "row.names", all=F)
  return(df)
}

但是,当我查看合并的数据集时,使用新创建的列('Row.names'),我注意到每行的数字不匹配。

Row.names Row.names   Row.names   Group   x x1 x2
1 1   1   119.081.240.943.588 259.775.831.650.296 0   23.866.286.096.227
10    106 193 118.588.864.584.922 845.466.938.787.896 0   0
100   188 267 532.470.728.304.433 565.616.257.412.307 0   373.683.435.824.118
1000  999 998 182.422.581.976.367 403.479.125.577.061 0   0
101   189 268 187.528.121.080.196 812.136.181.788.992 0   20
102   19  114 118.469.911.723.838 603.221.056.171.313 0   0
103   190 269 152.772.529.333.869 588.104.989.185.363 0   0
这是正常的吗?这是否意味着合并工作正常?

根据要求,以下是data.frames的一些部分:

structure(list(X1 = c(130.799442058057, 81.6156128579151, 74.5617156491735, 
52.5749755543994, 74.1027966606748, 67.7781088087785), X2 = c(9.35099377448125, 
0, 17.1038939434681, 10.4181409725891, 10.2255801019496, 11.3542481892001
), X3 = c(0, 0, 0, 0, 0, 0), GC = c(9.10284406227891, 14.8826264008001, 
14.8716322537003, 0, 17.9347676910113, 23.8861759361381), GS = c(0, 
0, 0, 0, 0, 0), Group_Functions = c(0, 0, 0, 0, 0, 0)), .Names = c("X1", 
"X2", "X3", "GC", "GS", "Group_Functions"), row.names = c("1", 
"2", "3", "4", "5", "6"), class = "data.frame")
       X1  X2 X3        GC GS Group_Functions
1 130.79944  9.350994    0  9.102844  0               0
2  81.61561  0.000000    0 14.882626  0               0
3  74.56172 17.103894    0 14.871632  0               0
4  52.57498 10.418141    0  0.000000  0               0
5  74.10280 10.225580    0 17.934768  0               0
6  67.77811 11.354248    0 23.886176  0               0
structure(list(Group = c(119.081240943588, 136.719503228408, 
162.420274223621, 193.370592817237, 97.3323039182813, 285.060721782651
)), .Names = "Group", row.names = c("1", "2", "3", "4", "5", 
"6"), class = "data.frame")
     Group
1 119.0812
2 136.7195
3 162.4203
4 193.3706
5  97.3323
6 285.0607
structure(list(X5 = c(25.9775831650296, 95.7144945621663, 
67.9981026443119, 84.350376440323, 112.058781027652, 47.0492919929386
), X7 = c(0, 0, 0, 0, 0, 0), X6 = c(23.866286096227, 
0, 24.544056579043, 23.5128741035179, 3.6304149198663, 4.22756099754193
), Other = c(0, 0, 0, 0, 0, 0), X8 = c(10.2354983826249, 
14.1412121409874, 12.8452387666377, 0, 14.2650082091202, 10.8114582861722
), X4 = c(0, 0, 0, 0, 0, 0)), .Names = c("X5", 
"X7", "X6", "Other", "X8", "X4"), row.names = c("1", 
"2", "3", "4", "5", "6"), class = "data.frame")
  X5 X7      X6 Other X8 X4
1    25.97758         0 23.866286     0   10.23550         0
2    95.71449         0  0.000000     0   14.14121         0
3    67.99810         0 24.544057     0   12.84524         0
4    84.35038         0 23.512874     0    0.00000         0
5   112.05878         0  3.630415     0   14.26501         0
6    47.04929         0  4.227561     0   10.81146         0

1 个答案:

答案 0 :(得分:0)

merge在加入rownames时会创建一个新列Row.names,并删除rownames。叹息。

所以,更新你的合并功能,让他们重新投入并继续!

MyMerge <- function(x, y){
  df <- merge(x, y, by= "row.names", all=F)
  rownames(df) <- df$Row.names
  df$Row.names <- NULL
  return(df)
}
Reduce(MyMerge, list(d1, d2, d3))