Re: [R] sort matrix based on a specific order
mat[match(ind, mat[, 2]), ] [,1] [,2] [1,] "y" "c" [2,] "x" "b" [3,] "z" "d" [4,] "w" "a" though you need to take care if you have cases where ind will contains letters that are not in mat[, 2] and so on (check ?match). Best, I On 10 Jan 2013, at 18:21, array chip 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@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. -- Dr Ioannis Kosmidis Department of Statistical Science, University College, London, WC1E 6BT, UK Webpage: http://www.ucl.ac.uk/~ucakiko __ R-help@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.
Re: [R] sort matrix based on a specific order
HI, Try this: mat[match(ind,mat[,2]),] # [,1] [,2] #[1,] "y" "c" #[2,] "x" "b" #[3,] "z" "d" #[4,] "w" "a" A.K. - Original Message - From: array chip To: "r-help@r-project.org" Cc: Sent: Thursday, January 10, 2013 1:21 PM Subject: [R] sort matrix based on a specific order 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@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. __ R-help@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.
Re: [R] sort matrix based on a specific order
Thank you all for the suggestions, fantastic! From: Rui Barradas Cc: "r-help@r-project.org" Sent: Thursday, January 10, 2013 10:32 AM Subject: Re: [R] sort matrix based on a specific order Hello, Try the following. order() gives you a permutation of the vector 'ind' and to order that permutation gives its inverse. mat <- cbind(c('w','x','y','z'),c('a','b','c','d')) ind <- c('c','b','d','a') ord <- order(ind) mat[order(ord), ] Hope this helps, Rui Barradas Em 10-01-2013 18:21, array chip escreveu: 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@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. [[alternative HTML version deleted]] __ R-help@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.
Re: [R] sort matrix based on a specific order
Hello, Try the following. order() gives you a permutation of the vector 'ind' and to order that permutation gives its inverse. mat <- cbind(c('w','x','y','z'),c('a','b','c','d')) ind <- c('c','b','d','a') ord <- order(ind) mat[order(ord), ] Hope this helps, Rui Barradas Em 10-01-2013 18:21, array chip escreveu: > 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@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. [[alternative HTML version deleted]] __ R-help@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.
Re: [R] sort matrix based on a specific order
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 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@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. __ R-help@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.
Re: [R] sort matrix based on a specific order
You can use factor() or match() to specify a particular order. E.g., > mat<-cbind(c('w','x','y','z'),c('a','b','c','d')) > ind<-c('c','b','d','a') > mat[ order(match(mat[,2], ind)), ] [,1] [,2] [1,] "y" "c" [2,] "x" "b" [3,] "z" "d" [4,] "w" "a" > mat[ order( factor(mat[,2], levels=ind) ), ] [,1] [,2] [1,] "y" "c" [2,] "x" "b" [3,] "z" "d" [4,] "w" "a" Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com > -Original Message- > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On > Behalf > Of array chip > Sent: Thursday, January 10, 2013 10:22 AM > To: r-help@r-project.org > Subject: [R] sort matrix based on a specific order > > 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@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.
Re: [R] sort matrix based on a specific order
Define them as factors with a specified order for your sorting. e.g. x <- factor(your_data, levels = c('c', 'b','d', 'a')) On Thu, Jan 10, 2013 at 1:21 PM, array chip 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@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. __ R-help@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.
[R] sort matrix based on a specific order
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@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.