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 h
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"
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 permutat
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,
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
;
[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
&
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
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,]
On 5/27/2009 8:39 AM, Paul Geeleher wrote:
I've got a matrix with 2 columns and n rows. I need to sort it first
by the values in column 1 ascending. Then for values which are the
same in column 1, sort by column 2 decending. For example:
You've seen a few ways. Here are some more:
1. Use the
See also this tip on the R wiki:
http://wiki.r-project.org/rwiki/doku.php?id=tips:data-frames:sort
Also available as the orderBy function in the doBy package.
Kevin Wright
On Wed, May 27, 2009 at 11:19 AM, Linlin Yan wrote:
> It's a very interesting problem. I just wrote a function for it:
>
It's a very interesting problem. I just wrote a function for it:
order.matrix <- function(m, columnsDecreasing = c('1'=FALSE), rows = 1:nrow(m))
{
if (length(columnsDecreasing) > 0)
{
col <- as.integer(names(columnsDecreasing[1]));
values <- sort(unique(m[rows, col]), decreasing=column
Nice. Works perfectly.
On Wed, May 27, 2009 at 2:03 PM, Henrique Dallazuanna wrote:
> Try this:
>
> cbind(sort(x[,1]), unlist(tapply(x[,2], x[,1], sort, decreasing = T)))
>
> On Wed, May 27, 2009 at 9:39 AM, Paul Geeleher
> wrote:
>>
>> I've got a matrix with 2 columns and n rows. I need to sort
Try this:
cbind(sort(x[,1]), unlist(tapply(x[,2], x[,1], sort, decreasing = T)))
On Wed, May 27, 2009 at 9:39 AM, Paul Geeleher wrote:
> I've got a matrix with 2 columns and n rows. I need to sort it first
> by the values in column 1 ascending. Then for values which are the
> same in column 1, s
I've got a matrix with 2 columns and n rows. I need to sort it first
by the values in column 1 ascending. Then for values which are the
same in column 1, sort by column 2 decending. For example:
2 .5
1 .3
1 .5
3 .2
Goes to:
1 .5
1 .3
2 .5
3 .2
This is easy to do in spreadsheet programs but I ca
IL PROTECTED]
> On Behalf Of Paul Geeleher
> Sent: 19 May 2008 12:07
> To: r-help@r-project.org
> Subject: [R] Sort matrix with duplicate row names alphabetically by
> rowname
>
> Hi,
>
> I've a matrix that contains 4 replicates of each rowname. (4 a's, 4 b
Try:
mat <- mat[order(rownames(mat)), ]
---
-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
On Behalf Of Paul Geeleher
Sent: 19 May 2008 12:07
To: r-help@r-project.org
Subject: [R] Sort matrix with duplicate row names alphabetically by
rowname
Hi,
I
Hi,
I've a matrix that contains 4 replicates of each rowname. (4 a's, 4
b's, 4 c's in no particular order) Like this:
#
c 32
a 1
b 4
c 87
c 34
b 54
a 23
a 12
b 9
a 3
b 87
c 43
There are a couple of more columns but I'm using the above as an example
I need to sort it so that the same rowna
M[ order(M[,1]), ]
G.
On Mon, Mar 10, 2008 at 01:56:45PM -0700, ermimi wrote:
>
> I have a matrix with 2 columns and n row. I need sort the matrix by the first
> column but the second row must be sort in the same order that the first
> column. Somebody know how I can sort this matrix.
> Thanks v
Here's an example. See ?order
mt <- matrix(sample(1:20), ncol = 2)
mt[order(mt[,1]),]
Best,
Erik Iverson
ermimi wrote:
> I have a matrix with 2 columns and n row. I need sort the matrix by the first
> column but the second row must be sort in the same order that the first
> column. Somebody kn
I have a matrix with 2 columns and n row. I need sort the matrix by the first
column but the second row must be sort in the same order that the first
column. Somebody know how I can sort this matrix.
Thanks very much
--
View this message in context:
http://www.nabble.com/SORT-MATRIX-tp15955823p1
20 matches
Mail list logo