Message-ID: <CAAxdm-6vKmvwYiOZjxZq-eNpghnbBPRNmhF6f0+NJ3dfO=Gj-Q@mail.gmail.com>
Date: 2013-01-10T18:30:50Z
From: jim holtman
Subject: sort matrix based on a specific order
In-Reply-To: <1357842101.979.YahooMailNeo@web122903.mail.ne1.yahoo.com>
more complete example
> mat<-cbind(c('w','x','y','z'),c('a','b','c','d'))
> matOrd <- mat[order(factor(mat[,2], levels = c('c', 'b', 'd','a'))), ]
> matOrd
[,1] [,2]
[1,] "y" "c"
[2,] "x" "b"
[3,] "z" "d"
[4,] "w" "a"
>
On Thu, Jan 10, 2013 at 1:21 PM, array chip <arrayprofile at yahoo.com> wrote:
> Hi I have a character matrix with 2 columns A and B, If I want to sort the matrix based on the column B, but based on a specific order of characters:
>
> mat<-cbind(c('w','x','y','z'),c('a','b','c','d'))
> ind<-c('c','b','d','a')
>
> I want "mat" to be sorted by the sequence in "ind":
>
> [,1] [,2]
> [1,] "y" "c"
> [2,] "x" "b"
> [3,] "z" "d"
> [4,] "w" "a"
>
> Is there any simple function that can do this?
>
> Thanks
>
> John
>
> [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
--
Jim Holtman
Data Munger Guru
What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.