R:根据列添加两个具有不同维度的矩阵

时间:2013-10-12 17:43:32

标签: r matrix

我有22个具有相同行数(即691)和不同列数(即22-25)的矩阵。我必须在每个矩阵中添加对应于同一行,相同列的值,从而得到维度为691 * 25的单个矩阵。

fullanno1 has 691 rows & 25 columns:
>colnames(fullanno1)
[1] "coding-notMod3"                "coding-synonymous"             "coding-synonymous-near-splice"
[4] "intergenic"                    "intron"                        "missense"                     
[7] "missense-near-splice"          "near-gene-3"                   "near-gene-5"                  
[10] "splice-3"                      "splice-5"                      "stop-gained"                  
[13] "stop-gained-near-splice"       "stop-lost"                     "utr-3"                        
[16] "utr-5"                         "CTCF"                          "E"                            
[19] "None"                          "PF"                            "R"                            
[22] "T"                             "TSS"                           "WE"                           
[25] "coding-notMod3-near-splice"   

fullanno2 has 691 rows and 22 columns:
>colnames(fullanno2)

[1] "coding-synonymous"             "coding-synonymous-near-splice" "intergenic"                   
[4] "intron"                        "missense"                      "missense-near-splice"         
[7] "near-gene-3"                   "near-gene-5"                   "splice-3"                     
[10] "splice-5"                      "stop-gained"                   "stop-lost"                    
[13] "utr-3"                         "utr-5"                         "CTCF"                         
[16] "E"                             "None"                          "PF"                           
[19] "R"                             "T"                             "TSS"                          
[22] "WE" 

每个矩阵都是带有数值的双矩阵。如何添加这两个矩阵,以便得到尺寸为691 * 25的第三个矩阵。因为fullanno2是三列短的,对于那些列,得到的矩阵将仅具有来自第一矩阵的值。

我的方法: 使用colnames的setdiff来获取较小矩阵中不存在的列,将它们cbind到较小的矩阵,其中0为值。然后添加两个矩阵。

> column.names<-setdiff(colnames(fullanno1),colnames(fullanno2))
[1] "coding-notMod3"             "stop-gained-near-splice"    "coding-notMod3-near-splice"
> column<-0
>cbind(fullanno2,column)
>colnames(fullanno2)[23]<-column.name[1]
>cbind(fullanno2,column)
>colnames(fullanno2)[24]<-column.name[2]
>cbind(fullanno2,column)
>colnames(fullanno2)[25]<-column.name[3]

但是这对所有矩阵来说都是乏味的。有什么建议吗?

2 个答案:

答案 0 :(得分:6)

您可以将matchcolnames一起使用。例如:

> m1<-matrix(1,3,5)
> colnames(m1)<-LETTERS[1:5]
> m2<-matrix(1:9,3,3)
> colnames(m2)<-c("D","A","C")
> m1
     A B C D E
[1,] 1 1 1 1 1
[2,] 1 1 1 1 1
[3,] 1 1 1 1 1
> m2
     D A C
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9

> m3<-m1
> mcol<-match(colnames(m2),colnames(m1))
> m3[,mcol]<-m3[,mcol]+m2
> m3
     A B  C D E
[1,] 5 1  8 2 1
[2,] 6 1  9 3 1
[3,] 7 1 10 4 1

答案 1 :(得分:3)

所以你想要将所有矩阵加起来以一个矩阵结束?一个简单但可能很慢(我怀疑,但它可能与你的矩阵没什么关系)的方法是使用plyrreshape2库。您可以从矩阵列表开始:

make.matrix <- function() {
  cols <- sample(month.name, runif(1, 2, 12))
  matrix(rnorm(length(cols)*10), 10, length(cols), dimnames=list(NULL, cols))
}

# Make 10 matrices filled with random numbers, having
# varying numbers of columns named after months
my.matrices <- replicate(10, make.matrix())

然后,您可以将所有矩阵融合到一个大数据帧中

matrix.df <- ldply(my.matrices, melt, varnames=c("row", "col"))
head(matrix.df)
#   row      col      value
# 1   1 February -0.4239145
# 2   2 February  1.1773608
# 3   3 February -2.9565403
# 4   4 February  0.3955096
# 5   5 February -0.3784917
# 6   6 February -0.6234579

然后将其重新投射到矩阵中。

sum.matrix <- acast(matrix.df, row ~ col, sum)