我有一个列表变量x9,它存储了三个元素
x9[[1]]
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 63 64 65 66 67
2 1 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0
x9[[2]]
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 63 64 65 66 67
0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
x9[[3]]
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 63 64 65 66 67
5 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
我的目标是取这些x9 [[1]],x9 [[2]],x9 [[3]],...中的每一个,并将它们转换为方阵。应使用零填充不匹配/不相关的行和列。例如,第一个元素x9 [[1]]应该转换为方形矩阵,如下所示
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 63 64 65 66 67
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
2 1 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0**
3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
10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 0 0 0 0 0 0 0 0 0
66 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
67 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
这应该在一个循环中发生,任何关于如何做到这一点的建议都会有所帮助。
structure(list(`0000000000000000000000000`=structure(list(`0`=c(0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),`1`=c(0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),`2`=c(0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),`3`=c(0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),`4`=c(0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),`5`=c(0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),`6`=c(0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),`7`=c(0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),`8`=c(0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),`9`=c(0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),`10`=c(0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),`11`=c(0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),`12`=c(0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),`13`=c(0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),`14`=c(0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),`15`=c(0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),`16`=c(0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),`17`=c(0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),`18`=c(0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),`19`=c(0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),`63`=c(0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),`64`=c(0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),`65`=c(0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),`66`=c(0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),`67`=c(0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)),.Names=c("0",
"1","2","3","4","5","6","7","8","9","10","11","12",
"13","14","15","16","17","18","19","63","64","65","66",
"67"),row.names=c("6","7","8","9","10","11","12","13",
"14","15","16","17","18","19","63","64","65","66","67"
),class="data.frame"),`0000000000000000000011111`=structure(list(
`0`=0,`1`=0,`2`=0,`3`=0,`4`=0,`5`=0,`6`=0,
`7`=0,`8`=0,`9`=0,`10`=0,`11`=0,`12`=0,
`13`=0,`14`=0,`15`=0,`16`=0,`17`=0,`18`=0,
`19`=0,`63`=1,`64`=1,`65`=1,`66`=1,`67`=1),.Names=c("0",
"1","2","3","4","5","6","7","8","9","10","11","12",
"13","14","15","16","17","18","19","63","64","65","66",
"67"),row.names="3",class="data.frame"),`0000000100000000111100000`=structure(list(
`0`=0,`1`=0,`2`=0,`3`=0,`4`=0,`5`=0,`6`=0,
`7`=1,`8`=0,`9`=0,`10`=0,`11`=0,`12`=0,
`13`=0,`14`=0,`15`=0,`16`=1,`17`=1,`18`=1,
`19`=1,`63`=0,`64`=0,`65`=0,`66`=0,`67`=0),.Names=c("0",
"1","2","3","4","5","6","7","8","9","10","11","12",
"13","14","15","16","17","18","19","63","64","65","66",
"67"),row.names="4",class="data.frame"),`0000001111100000000000000`=structure(list(
`0`=0,`1`=0,`2`=0,`3`=0,`4`=0,`5`=0,`6`=1,
`7`=1,`8`=1,`9`=1,`10`=1,`11`=0,`12`=0,
`13`=0,`14`=0,`15`=0,`16`=0,`17`=0,`18`=0,
`19`=0,`63`=0,`64`=0,`65`=0,`66`=0,`67`=0),.Names=c("0",
"1","2","3","4","5","6","7","8","9","10","11","12",
"13","14","15","16","17","18","19","63","64","65","66",
"67"),row.names="5",class="data.frame"),`0111110000000000000000000`=structure(list(
`0`=0,`1`=1,`2`=1,`3`=1,`4`=1,`5`=1,`6`=0,
`7`=0,`8`=0,`9`=0,`10`=0,`11`=0,`12`=0,
`13`=0,`14`=0,`15`=0,`16`=0,`17`=0,`18`=0,
`19`=0,`63`=0,`64`=0,`65`=0,`66`=0,`67`=0),.Names=c("0",
"1","2","3","4","5","6","7","8","9","10","11","12",
"13","14","15","16","17","18","19","63","64","65","66",
"67"),row.names="0",class="data.frame"),`1000000000011110000000000`=structure(list(
`0`=1,`1`=0,`2`=0,`3`=0,`4`=0,`5`=0,`6`=0,
`7`=0,`8`=0,`9`=0,`10`=0,`11`=1,`12`=1,
`13`=1,`14`=1,`15`=0,`16`=0,`17`=0,`18`=0,
`19`=0,`63`=0,`64`=0,`65`=0,`66`=0,`67`=0),.Names=c("0",
"1","2","3","4","5","6","7","8","9","10","11","12",
"13","14","15","16","17","18","19","63","64","65","66",
"67"),row.names="2",class="data.frame"),`1010110000000001000000000`=structure(list(
`0`=1,`1`=0,`2`=1,`3`=0,`4`=1,`5`=1,`6`=0,
`7`=0,`8`=0,`9`=0,`10`=0,`11`=0,`12`=0,
`13`=0,`14`=0,`15`=1,`16`=0,`17`=0,`18`=0,
`19`=0,`63`=0,`64`=0,`65`=0,`66`=0,`67`=0),.Names=c("0",
"1","2","3","4","5","6","7","8","9","10","11","12",
"13","14","15","16","17","18","19","63","64","65","66",
"67"),row.names="1",class="data.frame")),.Names=c("0000000000000000000000000",
"0000000000000000000011111","0000000100000000111100000","0000001111100000000000000",
"0111110000000000000000000","1000000000011110000000000","1010110000000001000000000"
))
答案 0 :(得分:3)
尝试
m2 <- matrix(0, nrow=ncol(lst1[[1]]), ncol=ncol(lst1[[1]]),
dimnames=list(colnames(lst1[[1]]), colnames(lst1[[1]])))
res <- lapply(lst1, function(x) {m2[rownames(x),colnames(x)] <- x; m2})
根据x9
输出
dput
x9New <- x9[-1] #deleting the first element as it was already square and without any `1`s
m2 <- matrix(0, nrow=ncol(x9New[[1]]), ncol=ncol(x9New[[1]]),
dimnames=list(colnames(x9New[[1]]), colnames(x9New[[1]])))
res1 <- lapply(x9New, function(x) {x1 <- as.matrix(x)
m2[rownames(x1), colnames(x1)] <- x1
m2})
unname(sapply(res1,dim))
# [,1] [,2] [,3] [,4] [,5] [,6]
#[1,] 25 25 25 25 25 25
#[2,] 25 25 25 25 25 25
lst1 <- list(structure(c(1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), .Dim = c(1L,
25L), .Dimnames = list("2", c("0", "1", "2", "3", "4", "5", "6",
"7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17",
"18", "19", "63", "64", "65", "66", "67"))), structure(c(0L,
1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), .Dim = c(1L, 25L), .Dimnames = list(
"0", c("0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"10", "11", "12", "13", "14", "15", "16", "17", "18", "19",
"63", "64", "65", "66", "67"))), structure(c(0L, 0L, 0L,
0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L), .Dim = c(1L, 25L), .Dimnames = list(
"5", c("0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"10", "11", "12", "13", "14", "15", "16", "17", "18", "19",
"63", "64", "65", "66", "67"))))