How to sort one column of an R data frame in ascending and the other in descending order?


Sorting of columns of an R data frame is not difficult but sometimes we want to sort them in opposite orders, for example, we might want to sort some columns in ascending order and some in descending order. This variation in sorting purpose makes it a little complicated. Therefore, we can use negation with sort function to sort the columns that we want to sort in descending order.

Example

Consider the below data frame −

 Live Demo

set.seed(111)
x1<-rnorm(20,2)
x2<-rpois(20,5)
x3<-sample(1:10,20,replace=TRUE)
x4<-sample(1:50,20)
x5<-rpois(20,10)
x6<-sample(1:100,20)
df<-data.frame(x1,x2,x3,x4,x5,x6)
df

Output

x1 x2 x3 x4 x5 x6
1 2.2352207 6 7 18 14 4
2 1.6692641 4 5 32 11 35
3 1.6883762 6 1 19 12 74
4 -0.3023457 8 10 33 14 65
5 1.8291240 5 10 25 8 9
6 2.1402782 4 7 27 10 55
7 0.5025733 4 4 28 7 83
8 0.9898116 7 4 44 10 47
9 1.0515244 6 1 11 9 88
10 1.5060378 7 9 31 13 67
11 1.8263259 6 6 12 12 48
12 1.5934012 5 8 41 13 44
13 3.8456363 1 3 6 8 27
14 2.3940541 4 10 39 9 58
15 2.7975285 12 8 38 13 10
16 0.4333346 5 10 50 11 99
17 1.9141490 5 1 9 6 40
18 1.6408605 5 9 40 6 32
19 0.8063910 2 10 35 8 5
20 2.3641867 7 10 36 6 15

Sorting column 1 in ascending order and sorting column 6 based on column 1 −

Example

df[order(df[,1],-df[,6]),]

Output

x1 x2 x3 x4 x5 x6
4 -0.3023457 8 10 33 14 65
16 0.4333346 5 10 50 11 99
7 0.5025733 4 4 28 7 83
19 0.8063910 2 10 35 8 5
8 0.9898116 7 4 44 10 47
9 1.0515244 6 1 11 9 88
10 1.5060378 7 9 31 13 67
12 1.5934012 5 8 41 13 44
18 1.6408605 5 9 40 6 32
2 1.6692641 4 5 32 11 35
3 1.6883762 6 1 19 12 74
11 1.8263259 6 6 12 12 48
5 1.8291240 5 10 25 8 9
17 1.9141490 5 1 9 6 40
6 2.1402782 4 7 27 10 55
1 2.2352207 6 7 18 14 4
20 2.3641867 7 10 36 6 15
14 2.3940541 4 10 39 9 58
15 2.7975285 12 8 38 13 10
13 3.8456363 1 3 6 8 27

Example

df[order(-df[,2],-df[,3]),]

Output

x1 x2 x3 x4 x5 x6
15 2.7975285 12 8 38 13 10
4 -0.3023457 8 10 33 14 65
20 2.3641867 7 10 36 6 15
10 1.5060378 7 9 31 13 67
8 0.9898116 7 4 44 10 47
1 2.2352207 6 7 18 14 4
11 1.8263259 6 6 12 12 48
3 1.6883762 6 1 19 12 74
9 1.0515244 6 1 11 9 88
5 1.8291240 5 10 25 8 9
16 0.4333346 5 10 50 11 99
18 1.6408605 5 9 40 6 32
12 1.5934012 5 8 41 13 44
17 1.9141490 5 1 9 6 40
14 2.3940541 4 10 39 9 58
6 2.1402782 4 7 27 10 55
2 1.6692641 4 5 32 11 35
7 0.5025733 4 4 28 7 83
19 0.8063910 2 10 35 8 5
13 3.8456363 1 3 6 8 27

Example

df[order(-df[,2],df[,5]),]

Output

x1 x2 x3 x4 x5 x6
15 2.7975285 12 8 38 13 10
4 -0.3023457 8 10 33 14 65
20 2.3641867 7 10 36 6 15
8 0.9898116 7 4 44 10 47
10 1.5060378 7 9 31 13 67
9 1.0515244 6 1 11 9 88
3 1.6883762 6 1 19 12 74
11 1.8263259 6 6 12 12 48
1 2.2352207 6 7 18 14 4
17 1.9141490 5 1 9 6 40
18 1.6408605 5 9 40 6 32
5 1.8291240 5 10 25 8 9
16 0.4333346 5 10 50 11 99
12 1.5934012 5 8 41 13 44
7 0.5025733 4 4 28 7 83
14 2.3940541 4 10 39 9 58
6 2.1402782 4 7 27 10 55
2 1.6692641 4 5 32 11 35
19 0.8063910 2 10 35 8 5
13 3.8456363 1 3 6 8 27

Updated on: 24-Aug-2020

152 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements