r - merging matrices with cbind and match: name of the last column is dropped -


i have list of matrices:

[[1]]                      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 

after merging list of matrices 1 merged matrix, following result:

                     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 

name of last column should "e" it's dropped. why? merging i'm using following code:

    reduce(function(a, b) {         ab <- cbind(a, b[match(rownames(a), rownames(b)), ])         ab[order(rownames(ab)), ]         ab     }, filter(is.matrix, x)) 

if use merge function result correct:

    reduce(function(a, b) as.matrix(transform(merge(a, b, = 'row.names', all.x = t), row.names = row.names, row.names = null)), filter(is.matrix, x)) 

output:

                     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 

try drop=false. when there single column, can keep structure intact drop=false can change matrix vector dropping dimension attribute.

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

Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Magento/PHP - Get phones on all members in a customer group -

session - Logging Out Using PHP -