矩阵的解构/矩阵的重排列

时间:2014-07-09 13:56:10

标签: r matrix rows reshape

请帮忙!

我有一个大矩阵,我想重新排列数据(或者我会称之为解构一个马克思?)

    M1   M2   M3   M4  
L1 "AA" "--" "GG" "CC"
L2 "AG" "CC" "--" "AA"
L3 "GG" "CG" "TT" "TT"
L4 "--" "GG" "CC" "TT"
L5 "AA" "--" "AA" "CC"
L6 "AT" "CC" "CT" "AA"
L7 "TT" "CG" "TA" "CC"

样本数据

test <- matrix(c("AA", "AG", "GG", "--","AA", "AT", "TT", "--","CC", "CG", "GG", "--","CC", "CG", "GG", "--","TT","CC","AA","CT","TA","CC","AA","TT","TT","CC","AA","CC"),nrow=7)
row.names(test)= c("L1", "L2", "L3", "L4", "L5", "L6", "L7")
colnames(test)= c("M1", "M2", "M3", "M4")

我需要重新排列并以下列格式获取数据

Line Marker testx
L1     M1    AA
L1     M2    --
L1     M3    GG
L1     M4    CC
L2     M1    AG
L2     M2    CC
L2     M3    --
L2     M4    AA
L3     M1    GG
L3     M2    CG
L3     M3    TT
L3     M4    TT
.
.
.

虽然我有一个冗长的解决方案(见下文),但在处理大型数据集时很难。请帮帮我!

testx<-c(test)
testx1<-data.frame(testx)
testx2<-cbind(Line = c("L1","L2","L3","L4","L5","L6","L7"), testx1)
testx3<-testx2[order(testx2$Line),]
testx4<-cbind(Marker = c("M1","M2","M3","M4"), testx3)
testx5 <- testx4[,c("Line", "Marker", "testx")]

3 个答案:

答案 0 :(得分:2)

我能想到的最简单方法是使用来自&#34; reshape2&#34;的melt

library(reshape2)
melt(test)
#    Var1 Var2 value
# 1    L1   M1    AA
# 2    L2   M1    AG
# 3    L3   M1    GG
# 4    L4   M1    --
# 5    L5   M1    AA
## <<SNIP>>
# 23   L2   M4    AA
# 24   L3   M4    TT
# 25   L4   M4    TT
# 26   L5   M4    CC
# 27   L6   M4    AA
# 28   L7   M4    CC

从那里开始,只需使用order即可获得&#34; Var1&#34;和&#34; Var2&#34;。

答案 1 :(得分:1)

data.frame(Line=rep(row.names(test), each=ncol(test)), Marker=rep(colnames(test), times = nrow(test)), testx=c(t(test)) )

答案 2 :(得分:0)

如果您首先将矩阵转换为data.frame,则可以使用包tidyr而不是reshape2

  library(tidyr)
  library(dplyr) ## for arrange()

  test1 <- data.frame(Line = rownames(test),test)

  test2 <- gather(test1,Marker,testx,M1:M4)

  head(arrange(test2,Line,Marker))

  Line Marker testx
1   L1     M1    AA
2   L1     M2    --
3   L1     M3    GG
4   L1     M4    CC
5   L2     M1    AG
6   L2     M2    CC

最后一步,arrange,只是按照示例中的方式对数据进行排序。正如@Ananda指出的那样,您可以轻松地对order进行相同的操作,例如

test2[order(test2$Line,test2$Marker),]