简体   繁体   English

R:用不同大小的矩阵列表生成矩阵

[英]R: Generate matrix with list of matrices of different size

I have a list of 1,000 matrices, whose two first elements are:我有一个包含 1,000 个矩阵的列表,其中两个第一个元素是:

> Bpam[1:2]
[[1]]
       cluster sil_width
D.var        1 0.7445190
Hy.adu       1 0.7233527
A.cra        0 0.8563551
Cu.cir       0 0.8483707
Sp.sp        0 0.8461553
E.gad        0 0.8368920
L.elo        0 0.8341050
A.mor        0 0.8219688
H.com        0 0.7046171
S.cad        0 0.5731629

[[2]]
       cluster sil_width
Hy.adu       1 0.5518385
D.var        1 0.1878755
Ab.gad       0 0.8183177
L.elo        0 0.7964595
A.cra        0 0.7964595
Cu.cir       0 0.7879583
A.mor        0 0.7667134
S.cad        0 0.5821773
H.com        0 0.5644358

Note that the number of rows differ ("Sp.sp" is absent in Bpam[[2]]. I would like to create a matrix of n columns and 1,000 rows (where n is the total number of different rownames across the list) to save the variable cluster of each matrix in the list and NAs if one of the rows is missing. I tried请注意,行数不同(Bpam[[2]] 中不存在“Sp.sp”。我想创建一个包含 n 列和 1,000 行的矩阵(其中 n 是列表中不同行名的总数)如果缺少其中一行,则保存列表和 NA 中每个矩阵的变量簇。我试过了

NA.matrix <- matrix(rep(NA, n*length(Bpam)), length(Bpam), n)
colnames(NA.matrix) <- A # char vector with names in n
# 
clus.memb.p <- sapply(1:length(Bpam), function(x) 
               NA.matrix[x, which(colnames(NA.matrix) %in% rownames(Bpam[[x]]))] 
               <- Bpam[[x]][,1])  

but its does not return a matrix.但它不返回矩阵。 Any help will be most welcome.任何帮助将是最受欢迎的。

We can merge by the row.names of the list of matrices .我们可以通过matrices listrow.names进行merge With all = TRUE , it will create NA by default if one of the row names is missing in the matrix使用all = TRUE ,如果matrix缺少行名称之一,它将默认创建NA

out <- Reduce(function(...) merge(..., by = "row.names", all = TRUE), Bpam)
row.names(out) <- out$Row.names
out <- out[, -1]

data数据

Bpam <- list(structure(c(1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0.744519, 0.7233527, 
0.8563551, 0.8483707, 0.8461553, 0.836892, 0.834105, 0.8219688, 
0.7046171, 0.5731629), .Dim = c(10L, 2L), .Dimnames = list(c("D.var", 
"Hy.adu", "A.cra", "Cu.cir", "Sp.sp", "E.gad", "L.elo", "A.mor", 
"H.com", "S.cad"), c("cluster", "sil_width"))), structure(c(1, 
1, 0, 0, 0, 0, 0, 0, 0.5518385, 0.1878755, 0.8183177, 0.7964595, 
0.7964595, 0.7879583, 0.7667134, 0.5821773), .Dim = c(8L, 2L), .Dimnames = list(
    c("Hy.adu", "D.var", "Ab.gad", "L.elo", "A.cra", "Cu.cir", 
    "A.mor", "S.cad"), c("cluster", "sil_width"))))

I found a very simple solution.我找到了一个非常简单的解决方案。 First, I modified my function to generate a list of matrices ordered alphabetically by row.names:首先,我修改了我的函数以生成按 row.names 的字母顺序排列的矩阵列表:

> Bpam[1:2]
[[1]]
       cluster  sil_width
A.cra        0 0.81960918
A.mor        0 0.83767035
Cu.cir       0 0.82537241
D.var        0 0.05898329
H.com        0 0.55435303
Hy.adu       1 0.00000000
L.elo        0 0.83984674
S.cad        0 0.81047726
Sp.sp        0 0.80335860

[[2]]
       cluster  sil_width
A.cra        0 0.85243545
A.mor        0 0.88501435
Cu.cir       0 0.87372261
D.var        0 0.02317251
E.gad        0 0.85797643
H.com        0 0.89236411
Hy.adu       1 0.00000000
S.cad        0 0.88181818
Sp.sp        0 0.88836713

Then然后

NA.matrix <- matrix(rep(NA, n*length(Bpam)), length(Bpam), n)
colnames(NA.matrix) <- sort(A)

So both the NA matrix and elements in the list are arranged alphabetically by col and row names, respectively.所以 NA 矩阵和列表中的元素分别按 col 和 row 名称的字母顺序排列。 Now I use a loop to fill in the NA matrix:现在我使用循环来填充 NA 矩阵:

for (i in 1:length(Bpam)) {
  NA.matrix[i, which(colnames(NA.matrix) %in% rownames(Bpam[[i]]))] <- Bpam[[i]][,1] 
  }

Done!完毕!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM