[R] How to join matrices of different row length from a list

A.N. Spiess a.spiess at uke.uni-hamburg.de
Fri Jan 7 12:50:08 CET 2011


Dear Emma,

there is a 'cbind.na', 'rbind.na' and 'data.frame.na' function in my qpcR
package.

library(qpcR)
matLis <- list(matrix(1:4, 2, 2), matrix(1:6, 3, 2),
     matrix(2:1, 1, 2))
do.call(cbind.na, matLis)

They are essentially the generic functions extended with an internal fill.

You might also want to try these examples:

## binding
cbind.na(1, 1:7) # the '1' (= shorter vector) is NOT recycled but filled
cbind.na(1:8, 1:7, 1:5, 1:10) # same with many vectors
rbind.na(1:8, 1:7, 1:5, 1:10) # or in rows

a <- matrix(rnorm(20), ncol = 4) # unequal size matrices
b <- matrix(rnorm(20), ncol = 5)
cbind.na(a, b) # works, in contrast to original cbind
rbind.na(a, b) # works, in contrast to original rbind

## data frame with unequal size vectors
data.frame.na(A = 1:7, B = 1:5, C = letters[1:3], 
              D = factor(c(1, 1, 2, 2))) 
              
## convert a list with unequal length list items
## to a data frame
z <- list(a = 1:5, b = letters[1:3], c = matrix(rnorm(20), ncol = 2))
do.call(data.frame.na, z)

-- 
View this message in context: http://r.789695.n4.nabble.com/How-to-join-matrices-of-different-row-length-from-a-list-tp3177212p3178991.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list