我有一个矩阵“垫子”,其中 012 个编码的 SNP 作为列,人作为行。例如:
> mat<-matrix(c("0","1","0","1","2","0","1","1","2"),3,byrow=T)
> rownames(mat)<-c("ID1","ID2","ID3")
> colnames(mat)<-c("rs123","rs333","rs9000")
> mat
rs123 rs333 rs9000
ID1 "0" "1" "0"
ID2 "1" "2" "0"
ID3 "1" "1" "2"
在不同的矩阵“mat2”中,我将各自的等位基因分为两列(即次要和主要等位基因),并将 SNP 作为行。
> mat2<-matrix(c("A","T","C","T","T","G"),3,byrow=T)
> rownames(mat2)<-c("rs123","rs333","rs9000")
> colnames(mat2)<-c("Allele_A","Allele_B")
> mat2
Allele_A Allele_B
rs123 "A" "T"
rs333 "C" "T"
rs9000 "T" "G"
现在我想将第一个矩阵中的 012 编码 SNP 重新编码为两列:如果它们的代码为零,它们应该是两个新列中各自的等位基因 A,如果是 A/B,如果是两个,则应该是 B/B . 在我的示例中,我想获得以下信息:
> mat3<-matrix(c("A","C","T","A","T","T","A","T","T","T","T","T","A","C","G","T","T","G" ),3,byrow=T)
> rownames(mat3)<-c("ID1","ID2","ID3")
> colnames(mat3)<-c("rs123_1","rs333_1","rs9000_1","rs123_2","rs333_2","rs9000_2")
> mat3
rs123_1 rs333_1 rs9000_1 rs123_2 rs333_2 rs9000_2
ID1 "A" "C" "T" "A" "T" "T"
ID2 "A" "T" "T" "T" "T" "T"
ID3 "A" "C" "G" "T" "T" "G"
你能帮我实现吗?先感谢您!