如果您使用for
循環,我不確定速度是否有任何差異。與@ flodel的數據集一起使用更多的矩陣
mat3 <- structure(c(1L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L,
0L, 1L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 1L, 1L), .Dim = c(5L,
5L), .Dimnames = list(c("a", "b", "c", "d", "e"), c("A", "B",
"D", "G", "H")))
mat4 <- structure(c(0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L,
0L, 1L, 0L, 0L, 1L, 1L, 1L, 1L, 0L, 1L, 0L, 0L), .Dim = c(5L,
5L), .Dimnames = list(c("a", "b", "c", "d", "e"), c("B", "C",
"D", "H", "I")))
lst1 <- mget(ls(pattern="^mat\\d+"))
mRes <- lst1[[1]]
for(i in seq_along(lst1)){
indx <- setdiff(colnames(lst1[[i]]), colnames(mRes))
mRes <-cbind(mRes, lst1[[i]][,indx, drop=FALSE])
mRes
}
mRes
# A B C D E F G H I
#a 1 0 0 0 0 0 1 0 1
#b 0 1 0 0 0 0 0 0 0
#c 0 1 0 0 0 0 1 1 1
#d 0 0 1 0 0 0 0 1 0
#e 0 0 0 1 0 0 0 1 0
如果列出現在多個矩陣中,如'A'在這裏,該怎麼辦?我們是否假設這些價值將永遠是一樣的?如果它們有區別呢? – flodel 2014-09-11 11:01:20