I have 22 matrices having equal number of rows (i.e. 691) and different number of columns (i.e. 22-25). I have to add the values corresponding to same row, same column in each of the matrices resulting in one single matrix of the dimension 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"
Each matrix is a double matrix with numerical values. How can I add these two matrices such that I get a third matrix with dimensions 691*25. Because fullanno2 is three columns short, for those columns the resulting matrix will have values only from the first matrix.
My approach: Take a setdiff of the colnames to get columns that are not present in the smaller matrix, cbind them to the smaller matrix with 0s as values. Then add the two matrices.
> 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]
But this is getting tedious for all the matrices. Any suggestions?