基于R中几个列名中的特定前缀重新排序矩阵列

时间:2015-04-13 08:49:11

标签: r matrix alphabetical

我在R中有matrix列名。

> colnames(m)
 [1] "caz_RNAi1_R1"     "caz_RNAi2_R1"     "cg1316_RNAi1_R1"  "cg1316_RNAi2_R1"  "cg4612_RNAi1_R1" 
 [6] "cg4612_RNAi2_R1"  "Dp1_RNAi1_R1"     "Dp1_RNAi2_R1"     "fmr1_RNAi1_R1"    "fmr1_RNAi2_R1"   
[11] "GFP_RNAi1_R1"     "GFP_RNAi2_R1"     "GFP_RNAi3_R1"     "GFP_RNAi4_R1"     "GFP_RNAi5_R1"    
[16] "GFP_RNAi6_R1"     "hrb87f_RNAi1_R1"  "hrb87f_RNAi2_R1"  "hrb98de_RNAi1_R1" "hrb98de_RNAi2_R1"

现在,一些列名称的前缀为GFP。我想重新排序矩阵列,以便在其名称中具有此前缀的列将是起始列,其余列将按字母顺序排列。

所以colnames(m)应该订购如下:

"GFP_a", "GFP_b", "GFP_c",..."GFP_z", "a", "b","c","d", ....

这样做的方法是什么?

2 个答案:

答案 0 :(得分:4)

你可以做到

 m[order(-(grepl('^GFP', m))+1L)]

其中m来自@Mamoun Benghezal的帖子。在示例中,它已按字母顺序排序,但如果不是

 set.seed(24)
 m1 <-sample(m)
 m1[order(m1)][order(-(grepl('^GFP',m1[order(m1)]))+1L)]

答案 1 :(得分:3)

你可以试试这个

m <-  c("caz_RNAi1_R1", "caz_RNAi2_R1", "cg1316_RNAi1_R1", "cg1316_RNAi2_R1", "cg4612_RNAi1_R1",
        "cg4612_RNAi2_R1", "Dp1_RNAi1_R1", "Dp1_RNAi2_R1", "fmr1_RNAi1_R1", "fmr1_RNAi2_R1",
        "GFP_RNAi1_R1", "GFP_RNAi2_R1", "GFP_RNAi3_R1", "GFP_RNAi4_R1", "GFP_RNAi5_R1",
        "GFP_RNAi6_R1", "hrb87f_RNAi1_R1",  "hrb87f_RNAi2_R1",  "hrb98de_RNAi1_R1", "hrb98de_RNAi2_R1")
sort(m[grep(pattern="^GFP", x = m )]) # beginning with GFP
## [1] "GFP_RNAi1_R1" "GFP_RNAi2_R1" "GFP_RNAi3_R1" "GFP_RNAi4_R1" "GFP_RNAi5_R1" "GFP_RNAi6_R1"
sort(m[-grep(pattern="^GFP", x = m )]) # do not begin by GFP
##     [1] "caz_RNAi1_R1"     "caz_RNAi2_R1"     "cg1316_RNAi1_R1"  "cg1316_RNAi2_R1"  "cg4612_RNAi1_R1"  "cg4612_RNAi2_R1"  "Dp1_RNAi1_R1"     "Dp1_RNAi2_R1"     "fmr1_RNAi1_R1"    "fmr1_RNAi2_R1"   
##    [11] "hrb87f_RNAi1_R1"  "hrb87f_RNAi2_R1"  "hrb98de_RNAi1_R1" "hrb98de_RNAi2_R1"
c(sort(m[grep(pattern="^GFP", x = m )]),  sort(m[-grep(pattern="^GFP", x = m )])) # ordered columns