简体   繁体   English

将多个等维矩阵的单元格值组合成一个矩阵,每个单元格是一个列表,作为 R 中输入矩阵的值

[英]Combining cell values of multiple equal dimension matrices to a single matrix with each cell is a list as values of the input matrices in R

Input: There are three input matrices of same dimensions 3 input matrices:输入:有三个相同维度的输入矩阵 3 个输入矩阵:

              GeneA             GeneB          
GeneA          31                  4           
GeneB           5                  8 

              GeneA             GeneB          
GeneA           5                 14           
GeneB           5                  8 


              GeneA             GeneB          
GeneA          30                 14           
GeneB           45                 7 

output:输出:

                GeneA             GeneB          
GeneA          {31,5,30}         {4,14,14}          
GeneB          {5,5,45}           {8,8,7} 

one matrix with cell value as a list of the values from the input matrices.一个矩阵,其单元格值作为输入矩阵中的值的列表。

You can use mapply , and then assign the structure and dimnames as those from matrix1您可以使用mapply ,然后将结构和 dimnames 分配为matrix1中的那些

matrix(
  mapply(paste, matrix1, matrix2, matrix3, MoreArgs=list(sep=",")), 
  nrow=nrow(matrix1),
  ncol=ncol(matrix1),
  dimnames = dimnames(matrix1)
)

Output:输出:

      GeneA     GeneB    
GeneA "31,5,30" "4,14,14"
GeneB "5,5,45"  "8,8,7"  

You would need to hold each resulting vector within a list in a matrix.您需要将每个结果向量保存在矩阵的列表中。 If your matrices are called m1 , m2 and m3 , then you can do:如果您的矩阵称为m1m2m3 ,那么您可以这样做:

m <- `dimnames<-`(matrix(lapply(1:4, function(i) c(m1[i], m2[i], m3[i])), 2),
                  dimnames(m1))

So that we have:所以我们有:

m
#>       GeneA     GeneB    
#> GeneA integer,3 integer,3
#> GeneB integer,3 integer,3

and

m[1, 1]
#> [[1]]
#> [1] 31  5 30

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

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