使用 cbind 和 match 合并矩阵:删除最后一列的名称
merging matrices with cbind and match: name of the last column is dropped
我有一个矩阵列表:
[[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
尝试drop=FALSE
。当只有一个列时,您可以使用 drop=FALSE
保持结构完整,因为它可以通过删除维度属性从 matrix
更改为 vector
。
Reduce(function(a,b){
ab <- cbind(a, b[match(rownames(a), rownames(b)), ,drop=FALSE])
ab <- ab[order(rownames(ab)), ]
ab
},Filter(is.matrix, lst))
# 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
我有一个矩阵列表:
[[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
尝试drop=FALSE
。当只有一个列时,您可以使用 drop=FALSE
保持结构完整,因为它可以通过删除维度属性从 matrix
更改为 vector
。
Reduce(function(a,b){
ab <- cbind(a, b[match(rownames(a), rownames(b)), ,drop=FALSE])
ab <- ab[order(rownames(ab)), ]
ab
},Filter(is.matrix, lst))
# 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