我有一个矩阵列表:
[[1]]
a b
X2005.06 NA 2504179
X2006.06 NA 3746905
X2007.06 NA 5468607
X2008.06 NA 6664545
X2009.06 NA 7339310
X2010.06 4602635 5587625
X2011.06 5188408 6498543
X2012.06 5190519 6831415
X2013.06 5246169 8214118
X2014.06 5856140 9384405
[[2]]
c d
X2005.06 354981 1729284
X2006.06 423136 1902213
X2007.06 602389 1902213
X2008.06 557095 1902213
X2009.06 -136485 2187545
X2010.06 469650 2665408
X2011.06 761031 2800000
X2012.06 246340 2800000
X2013.06 223661 2800000
X2014.06 698216 2800000
TTM 1340696 3052000
[[3]]
e
X2005.06 489915
X2006.06 464280
X2007.06 576947
X2008.06 799941
X2009.06 817066
X2010.06 1261012
X2011.06 1134682
X2012.06 257973
X2013.06 1011744
X2014.06 1119384
TTM 163454
将此矩阵列表合并为一个合并矩阵后,我得到以下结果:
a b c d
X2005.06 NA 2504179 354981 1729284 489915
X2006.06 NA 3746905 423136 1902213 464280
X2007.06 NA 5468607 602389 1902213 576947
X2008.06 NA 6664545 557095 1902213 799941
X2009.06 NA 7339310 -136485 2187545 817066
X2010.06 4602635 5587625 469650 2665408 1261012
X2011.06 5188408 6498543 761031 2800000 1134682
X2012.06 5190519 6831415 246340 2800000 257973
X2013.06 5246169 8214118 223661 2800000 1011744
X2014.06 5856140 9384405 698216 2800000 1119384
最后一列的名称应为“e”,但已删除。为什么?对于合并,我使用以下代码:
Reduce(function(a, b) {
ab <- cbind(a, b[match(rownames(a), rownames(b)), ])
ab[order(rownames(ab)), ]
ab
}, Filter(is.matrix, x))
如果我使用merge
函数,那么结果是正确的:
Reduce(function(a, b) as.matrix(transform(merge(a, b, by = 'row.names', all.x = T), row.names = Row.names, Row.names = NULL)), Filter(is.matrix, x))
输出:
a b c d e
X2005.06 NA 2504179 354981 1729284 489915
X2006.06 NA 3746905 423136 1902213 464280
X2007.06 NA 5468607 602389 1902213 576947
X2008.06 NA 6664545 557095 1902213 799941
X2009.06 NA 7339310 -136485 2187545 817066
X2010.06 4602635 5587625 469650 2665408 1261012
X2011.06 5188408 6498543 761031 2800000 1134682
X2012.06 5190519 6831415 246340 2800000 257973
X2013.06 5246169 8214118 223661 2800000 1011744
X2014.06 5856140 9384405 698216 2800000 1119384