【问题标题】:row names do not match after merge合并后行名不匹配
【发布时间】:2017-10-13 08:37:39
【问题描述】:

我有 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

【问题讨论】:

  • 请包含一些示例数据,例如使用dput(head(df))
  • @Jimbou 给你
  • 使用Reduce(MyMerge, list(d1, d2, d3))没问题
  • 我遇到了问题,row.names 不匹配。

标签: r merge


【解决方案1】:

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

所以,更新你的合并功能,把它们放回去继续!

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))

【讨论】:

  • 它返回正确的合并数据,但没有行名。
  • 什么不返回带有行名的数据,你是如何检查这种情况的?
  • 运行MyMerge后返回的表,不包含行名
  • 好的,但是如何你确定没有行名?
  • 输出表没有行名?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-21
  • 2017-12-12
  • 1970-01-01
  • 1970-01-01
  • 2012-03-13
相关资源
最近更新 更多