# How to sort a vector in R based on manual position of elements?

R ProgrammingServer Side ProgrammingProgramming

To sort a vector based on manual position of elements, we can use order function along with the factor function. The factor function will help us to arrange the vector elements in the order we want by defining the levels as vector elements and order function will order them. Check out the below examples to understand how it works.

## Example1

Live Demo

> x1<-sample(0:4,200,replace=T)
> x1

## Output

[1] 0 1 0 0 1 2 1 2 3 1 2 2 2 4 3 1 4 1 0 1 1 3 3 0 0 4 4 2 4 2 4 2 0 4 0 1 1
[38] 4 2 0 1 0 3 0 4 3 2 0 0 3 2 0 1 0 0 0 4 2 2 4 2 0 4 3 3 1 3 1 2 3 0 2 2 0
[75] 1 1 4 4 4 0 4 0 3 4 0 3 1 4 2 2 3 0 2 4 3 1 2 1 1 3 1 3 4 4 3 1 1 4 4 1 0
[112] 2 2 4 0 0 2 2 1 2 4 4 0 4 1 2 0 3 0 0 4 1 2 4 0 1 1 0 4 1 3 2 0 4 4 2 4 1
[149] 3 4 4 3 2 1 2 2 0 3 4 1 0 1 4 4 0 4 0 2 3 4 1 4 3 2 1 2 4 0 1 3 0 4 2 4 4
[186] 4 3 2 3 4 1 1 0 0 4 0 4 4 0 2

## Example

> x1[order(factor(x1,levels=c(3,1,0,4,2)))]

## Output

[1] 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1 1 1 1 1 1 1 1 1
[38] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0
[75] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[112] 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
[149] 4 4 4 4 4 4 4 4 4 4 4 4 4 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
[186] 2 2 2 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1 4 3
[151] 2 6 1 6 2 5 5 7 8 10 5 5 1 9 10 10 3 6 10 5 3 4 2 10 9
[176] 2 7 4 3 3 9 3 5 1 3 8 1 5 7 8 9 8 3 5 2 1 7 6 2 9
[201] 9 5 6 7 10 7 3 6 5 2 5 8 1 3 9 1 5 10 7 3 9 3 3 7 3
[226] 2 7 3 8 1 6 2 8 3 3 9 8 10 3 10 10 8 8 2 6 9 7 3 6 6
[251] 10 9 4 2 9 8 10 7 10 2 1 8 10 10 7 2 10 8 2 5 10 4 10 8 9
[276] 1 9 4 10 4 1 2 7 1 9 2 5 3 5 3 6 9 9 6 8 7 10 5 6 9

## Example

> x4[order(factor(x4,levels=c(2,4,6,8,10,1,3,5,7,9)))]

## Output

[1] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 4 4 4 4 4
[26] 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 6 6 6 6
[51] 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6
[76] 6 6 6 6 6 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8
[101] 8 8 8 8 8 8 8 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10
[126] 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 1 1 1 1 1 1
[151] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[176] 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
[201] 3 3 3 3 3 3 3 3 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
[226] 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 7 7 7 7 7 7 7 7 7
[251] 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 9 9 9 9 9 9 9 9
[276] 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9

Example5

Live Demo

> x5<-sample(21:25,200,replace=T)
> x5

## Output

[1] 25 22 25 24 24 25 24 24 22 22 25 25 24 21 21 24 24 24 25 22 24 22 25 22 24
[26] 21 24 24 21 21 25 22 23 21 21 22 23 22 22 25 23 25 22 21 22 25 24 25 21 24
[51] 23 21 22 22 24 24 24 24 25 24 23 21 25 24 22 23 23 24 25 21 23 23 21 25 22
[76] 23 21 25 23 21 23 21 25 24 22 24 22 22 21 21 21 24 22 25 23 22 23 24 24 22
[101] 23 21 24 25 24 22 25 25 22 22 22 25 25 22 24 25 22 22 24 23 23 21 22 25 25
[126] 21 24 23 25 25 23 22 21 21 22 24 24 24 25 24 24 22 23 22 21 23 21 24 25 23
[151] 25 22 21 25 25 23 23 23 24 21 24 22 21 22 24 25 22 24 23 22 25 24 23 23 24
[176] 25 24 25 23 23 21 22 22 24 21 22 24 21 22 22 24 24 25 24 21 22 25 23 23 22

## Example

> x5[order(factor(x5,levels=c(25,24,21,22,23)))]

## Output

[1] 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25
[26] 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 24 24 24 24 24 24 24 24 24 24
[51] 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24
[76] 24 24 24 24 24 24 24 24 24 24 24 24 24 24 21 21 21 21 21 21 21 21 21 21 21
[101] 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 22 22 22
[126] 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22
[151] 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 23 23 23 23 23 23 23
[176] 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23
Published on 05-Mar-2021 06:33:56