Reputation: 2269
I have a matrix
in R with column names.
> 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"
Now, some of the column names has prefix GFP
. I want to reorder the matrix columns so that the columns having this prefix in their names would be the beginning columns and rest of the columns would be alphabetically ordered among themselves.
So colnames(m)
should be ordered something like:
"GFP_a", "GFP_b", "GFP_c",..."GFP_z", "a", "b","c","d", ....
What's the way to do this?
Upvotes: 3
Views: 1123
Reputation: 887078
You could do
m[order(-(grepl('^GFP', m))+1L)]
where m
is from @Mamoun Benghezal's post. In the example, it is already ordered alphabetically, but in case it is not
set.seed(24)
m1 <-sample(m)
m1[order(m1)][order(-(grepl('^GFP',m1[order(m1)]))+1L)]
Upvotes: 4
Reputation: 5314
you can try this
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
Upvotes: 3