Re: [R] Specifying the ordering of a vector

2012-04-09 Thread crmnaw
Thank you very much! This worked. 

--
View this message in context: 
http://r.789695.n4.nabble.com/Specifying-the-ordering-of-a-vector-tp4542766p4542907.html
Sent from the R help mailing list archive at Nabble.com.

__
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] Specifying the ordering of a vector

2012-04-09 Thread Duncan Murdoch

On 09/04/2012 9:58 AM, crmnaw wrote:

Hi,

Thanks for your reply. In the example I gave in the original post, your code
works. But for others, it doesn't and I'm not sure why it works for some
cases and not for others. For example:

x<-c(0.04,0.07,0.20,0.35,0.55,0.70)
order(x)
[1] 1 2 3 4 5 6

Let's say I want y to be in the order 1-2-5-3-6-4. So I do:

y<-x[c(1,2,5,3,6,4)]
y
[1] 0.04 0.07 0.55 0.20 0.70 0.35
order(y)
[1] 1 2 4 6 3 5

So, y isn't in the order I want it to be in. Any help that can be provided
would be greatly appreciated.


Okay, I didn't understand what you meant.

You should think of the result of order(y) as a permutation to apply to 
y so that it is in sorted order.  That is,


y[order(y)]

will always be in sorted order.  So what you want is a permutation of x 
that will be put into sorted order by y[c(1,2,5,3,6,4)], i.e. you want 
the inverse of the permutation c(1,2,5,3,6,4).  Turns out that if you 
apply order to a permutation you get its inverse.  So what you want is


y <- x[order(c(1,2,5,3,6,4))]

Duncan Murdoch

__
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] Specifying the ordering of a vector

2012-04-09 Thread crmnaw
Hi,

Thanks for your reply. In the example I gave in the original post, your code
works. But for others, it doesn't and I'm not sure why it works for some
cases and not for others. For example:

x<-c(0.04,0.07,0.20,0.35,0.55,0.70)
order(x)
[1] 1 2 3 4 5 6 

Let's say I want y to be in the order 1-2-5-3-6-4. So I do:

y<-x[c(1,2,5,3,6,4)]
y
[1] 0.04 0.07 0.55 0.20 0.70 0.35
order(y)
[1] 1 2 4 6 3 5

So, y isn't in the order I want it to be in. Any help that can be provided
would be greatly appreciated. 


--
View this message in context: 
http://r.789695.n4.nabble.com/Specifying-the-ordering-of-a-vector-tp4542766p4542815.html
Sent from the R help mailing list archive at Nabble.com.

__
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] Specifying the ordering of a vector

2012-04-09 Thread Duncan Murdoch

On 09/04/2012 9:38 AM, crmnaw wrote:

Hi,

I'm trying to create a vector (or matrix row) with a specific ordering. For
example, I have the following vector:

x<-c(0.1,0.2,0.3,0.4,0.5,0.6)

that has order

order(x)
[1] 1 2 3 4 5 6

I want another vector that has the same values as x, but with a different
ordering. For example, I want y to have values 0.1, 0.2, etc. but in the
order 1-2-5-6-3-4. The answer would be

y
[1] 0.1 0.2 0.5 0.6 0.3 0.4

Any help would be greatly appreciated. Thanks.


x[c(1,2,5,6,3,4)]

will do it.

Duncan Murdoch

__
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] Specifying the ordering of a vector

2012-04-09 Thread crmnaw
Hi, 

I'm trying to create a vector (or matrix row) with a specific ordering. For
example, I have the following vector:

x<-c(0.1,0.2,0.3,0.4,0.5,0.6) 

that has order

order(x)
[1] 1 2 3 4 5 6

I want another vector that has the same values as x, but with a different
ordering. For example, I want y to have values 0.1, 0.2, etc. but in the
order 1-2-5-6-3-4. The answer would be

y
[1] 0.1 0.2 0.5 0.6 0.3 0.4

Any help would be greatly appreciated. Thanks. 

--
View this message in context: 
http://r.789695.n4.nabble.com/Specifying-the-ordering-of-a-vector-tp4542766p4542766.html
Sent from the R help mailing list archive at Nabble.com.

__
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.