我有 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]
但这对于所有矩阵来说都变得乏味。有什么建议么?