I have a list containing 5 matrices, each of different sizes, and I would like to combine them all using row names.
Here is a reproducible example of my list (I am using igraph_0.6.5-2 on R version 3.0.1):
x <- list( as.matrix(c(1,4)), as.matrix(c(3,19,11)), as.matrix(c(3,9,8,5)), as.matrix(c(3,10,8,87,38,92)), as.matrix(c(87,8,8,87,38,92)) ) colnames(x[[1]]) <- c("P1") colnames(x[[2]]) <- c("P2") colnames(x[[3]]) <- c("P3") colnames(x[[4]]) <- c("P4") colnames(x[[5]]) <- c("P5") rownames(x[[1]]) <- c("A","B") rownames(x[[2]]) <- c("B","C","D") rownames(x[[3]]) <- c("A","B", "E", "F") rownames(x[[4]]) <- c("A","F","G","H","I","J" ) rownames(x[[5]]) <- c("B", "H","I","J", "K","L")
which gives me the following list:
> x [[1]] P1 A 1 B 4 [[2]] P2 B 3 C 19 D 11 [[3]] P3 A 3 B 9 E 8 F 5 [[4]] P4 A 3 F 10 G 8 H 87 I 38 J 92 [[5]] P5 B 87 H 8 I 8 J 87 K 38 L 92
I would like to get something like this:
> P1 P2 P3 P4 P5 A 1 na 3 3 na B 4 3 9 na 87 C na 19 na na na D na 11 na na na E na na 8 na na F na na 5 10 na G na na na 8 na H na na na 87 na I na na na 38 8 J na na na 92 87 K na na na na 38 L na na na na 92
Combining them with the do.call function:
y <- do.call(merge,c(x, by="row.names",all=TRUE))
gives the following error:
Error in fix.by(by.x, x) : 'by' must match numbers of columns
Any help is appreciated. Thanks!