[R] Specifying the ordering of a vector

Duncan Murdoch murdoch.duncan at gmail.com
Mon Apr 9 15:44:05 CEST 2012


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



More information about the R-help mailing list