R:如何在LOOP中匹配/合并2个不同维数(nrow/ncol)的矩阵? [英] R: How to match/join 2 matrices of different dimensions (nrow/ncol) in a LOOP?

查看:270
本文介绍了R:如何在LOOP中匹配/合并2个不同维数(nrow/ncol)的矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了我先前的问题 R:如何匹配/合并2个不同维数(nrow/ncol)的矩阵?我需要一个循环,因为我有1000多个矩阵的列表.我决定使用代码(从答案中得出)

In addition to my earlier question R: How to match/join 2 matrices of different dimensions (nrow/ncol)? I need a loop as I have a list of more than 1000 matrices. I decide to use the code (from the answers)

full_matrix[rownames(full_matrix) %in% rownames(small_matrix), 
            colnames(full_matrix) %in% colnames(small_matrix)] <- small_matrix

在我的情况下,对于单个矩阵匹配来说效果很好.但是,对于在matrices"small_matrix"的list循环中实现此代码,我遇到了问题.这是我的代码:

which worked fine in my case for a single matrix match. However, I have problems implementing this code into a loop for the list of matrices "small_matrix". Here my code:

full_matrix <- list()
for(i in 1:length(small_matrix)) 
{
  full_matrix[i][rownames(full_matrix[i]) %in% rownames(small_matrix[i]), 
                 colnames(full_matrix[i]) %in% colnames(small_matrix[i])] <- small_matrix[i]
}

我得到错误:incorrect number of subscripts on matrix,这可能是由于我对循环的基本知识中的错误索引所致.感谢您的帮助.谢谢

I get the error: incorrect number of subscripts on matrix, which is probably due to a wrong indexing in my basic knowledge of loops. I appreciate any help. Thanks

根据@Jim M.的回答进行的新 您的答案在此示例中效果很好,但还应该像以前的.在这里,您可以找到代码示例:

NEW EDIT according to answer of @Jim M.: Your answer works fine for this example but the small_matrix(ces) should be created as well in a loop like in this former question. Here you find the code example:

library(abind)

a = c("A", "B", "C", "D", "E", "F")
full_matrix = array(dim=c(6,6,2))
dimnames(full_matrix) <- list(levels(as.factor(a)), levels(as.factor(a)), 
                              c("mat1","mat2"))

@Jim M.建议.这是新代码:

as @Jim M. advises. And here the new code:

##### new part for small matrix
df_import <- data.frame(OC = c("A", "A", "B", "B", "C", "C", "D", "D"),
                        TC = c("Z", "Z", "Y", "Y", "X", "X", "W", "W"),
                        Value = c(1,2,3,4,5,6,7,8),
                        Year = c(2010,2011))
Import_Year <- split(df_import, df_import$Year)

library(reshape2)
small_matrix <- list()
for(i in 1:length(unique(Import_Year))) 
{
  small_matrix[[length(small_matrix)+1]] <- dcast(OC ~ TC, data =Import_Year[[i]], value.var = "Value")
}
small_matrix
##### end new part

,然后是@Jim的for循环. M:

and than further the for-loop of @Jim. M:

for(i in seq_along(small_matrix_list)){
  afill(full_matrix[, , i], local= TRUE ) <- small_matrix[[i]]   
}

但是,我收到以下错误消息:

However, I get the following error message:

Error in `afill<-.default`(`*tmp*`, local = TRUE, value = list(OC = 1:4,  : 
  'x' must have names on dimensions corresponding to those in 'value'

有什么想法吗?谢谢

推荐答案

我将创建完整的矩阵"作为3维数组而不是列表,然后通过循环用较小的矩阵列表填充此数组通过数组的3d维的相应索引.

I would create your full "matrix" as a 3-dimensional array rather than a list, and then fill in this array with a list of smaller matrices by looping through the corresponding index of the 3d dimension of the array.

library(abind)

a = c("A", "B", "C", "D", "E", "F")
full_matrix = array(dim=c(6,6,2))
dimnames(full_matrix) <- list(levels(as.factor(a)), levels(as.factor(a)), 
    c("mat1","mat2"))

small_matrix_1 = matrix(c(2, 4, 3, 1, 5, 7, 3, 1, 6), nrow=3, ncol=3)
dimnames(small_matrix_1) <- list(c("B","C","E"), c("A","B","F"))

small_matrix_2 = matrix(c(20, 40, 30, 10, 50, 70, 30, 10, 60), nrow=3, ncol=3)
dimnames(small_matrix_2) <- list(c("B","C","E"), c("A","B","F"))

small_matrix_list <- list(small_matrix_1, small_matrix_2)

for(i in seq_along(small_matrix_list)){
    afill(full_matrix[, , i], local= TRUE ) <- small_matrix_list[[i]]   
}

给予 full_matrix 第三个索引对应每个小矩阵.

giving full_matrix with the 3rd index corresponding to each of the small matrices.

, , mat1

   A  B  C  D  E  F
A NA NA NA NA NA NA
B  2  1 NA NA NA  3
C  4  5 NA NA NA  1
D NA NA NA NA NA NA
E  3  7 NA NA NA  6
F NA NA NA NA NA NA

    , , mat2

   A  B  C  D  E  F
A NA NA NA NA NA NA
B 20 10 NA NA NA 30
C 40 50 NA NA NA 10
D NA NA NA NA NA NA
E 30 70 NA NA NA 60
F NA NA NA NA NA NA

而不是像上一个问题那样使用dcast创建data.frames,我将数据转换为数组(acast),以便它们可以插入到更大的数组中.

Instead of creating data.frames with dcast as in your previous question, I would cast the data as arrays (acast) so that they can inserted into the larger array.

library(reshape2) 
small_matrix <- vector(mode = "list", length = 2) # Pre-allocate space in the list

for (i in seq(Import_Year)){
  small_matrix[[i]] <- acast(OC ~ TC, data = Import_Year[[i]], value.var = "Value")
}

这篇关于R:如何在LOOP中匹配/合并2个不同维数(nrow/ncol)的矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆