How to sort in R with sort function and order function

How to sort in R ?

In this chapter we will learn how to sort a data frame and vector in R.·

  • Sorting a vector in R can be done with sort () function.
  • Sorting a data frame in R can be done with order() function.

How to sort in R – sorting a data frame:

Lets use mtcars data to describe sorting a data frame in R with order() function.

Sort by single column in R:

Lets sort mtcars by mpg column

# sort by mpg - using order function in R
mtcars[order(mtcars$mpg),]
output:
mpgcyldisphpdratwtqsecvsamgearcarb
Cadillac Fleetwood10.484722052.935.2517.980034
Lincoln Continental10.4846021535.42417.820034
Camaro Z2813.383502453.733.8415.410034
Duster 36014.383602453.213.5715.840034
Chrysler Imperial14.784402303.235.34517.420034
Maserati Bora1583013353.543.5714.60158
Merc 450SLC15.28275.81803.073.78180033
AMC Javelin15.283041503.153.43517.30032
Dodge Challenger15.583181502.763.5216.870032
Ford Pantera L15.883512644.223.1714.50154
Merc 450SE16.48275.81803.074.0717.40033
Merc 450SL17.38275.81803.073.7317.60033
Merc 280C17.86167.61233.923.4418.91044
Valiant18.162251052.763.4620.221031
Hornet Sportabout18.783601753.153.4417.020032
Merc 28019.26167.61233.923.4418.31044
Pontiac Firebird19.284001753.083.84517.050032
Ferrari Dino19.761451753.622.7715.50156
Mazda RX42161601103.92.6216.460144
Mazda RX4 Wag2161601103.92.87517.020144
Hornet 4 Drive21.462581103.083.21519.441031
Volvo 142E21.441211094.112.7818.61142
Toyota Corona21.54120.1973.72.46520.011031
Datsun 71022.84108933.852.3218.611141
Merc 23022.84140.8953.923.1522.91042
Merc 240D24.44146.7623.693.19201042
Porsche 914-2264120.3914.432.1416.70152
Fiat X1-927.3479664.081.93518.91141
Honda Civic30.4475.7524.931.61518.521142
Lotus Europa30.4495.11133.771.51316.91152
Fiat 12832.4478.7664.082.219.471141
Toyota Corolla33.9471.1654.221.83519.91141

Sort by more than one column in R: 

Lets sort mtcars by carb  and then by cyl  

# sort by carb and cyl - using order function in R
mtcars[order(mtcars$carb,mtcars$cyl),]
mpgcyldisphpdratwtqsecvsamgearcarb
Datsun 71022.84108933.852.3218.611141
Fiat 12832.4478.7664.082.219.471141
Toyota Corolla33.9471.1654.221.83519.91141
Toyota Corona21.54120.1973.72.46520.011031
Fiat X1-927.3479664.081.93518.91141
Hornet 4 Drive21.462581103.083.21519.441031
Valiant18.162251052.763.4620.221031
Merc 240D24.44146.7623.693.19201042
Merc 23022.84140.8953.923.1522.91042
Honda Civic30.4475.7524.931.61518.521142
Porsche 914-2264120.3914.432.1416.70152
Lotus Europa30.4495.11133.771.51316.91152
Volvo 142E21.441211094.112.7818.61142
Hornet Sportabout18.783601753.153.4417.020032
Dodge Challenger15.583181502.763.5216.870032
AMC Javelin15.283041503.153.43517.30032
Pontiac Firebird19.284001753.083.84517.050032
Merc 450SE16.48275.81803.074.0717.40033
Merc 450SL17.38275.81803.073.7317.60033
Merc 450SLC15.28275.81803.073.78180033
Mazda RX42161601103.92.6216.460144
Mazda RX4 Wag2161601103.92.87517.020144
Merc 28019.26167.61233.923.4418.31044
Merc 280C17.86167.61233.923.4418.91044
Duster 36014.383602453.213.5715.840034
Cadillac Fleetwood10.484722052.935.2517.980034
Lincoln Continental10.4846021535.42417.820034
Chrysler Imperial14.784402303.235.34517.420034
Camaro Z2813.383502453.733.8415.410034
Ford Pantera L15.883512644.223.1714.50154
Ferrari Dino19.761451753.622.7715.50156
Maserati Bora1583013353.543.5714.60158

Sort one column by descending and other column by ascending in R: 

In mtcars table, lets sort carb by ascending and cyl by descending. In order to sort by descending include a minus sign in front 

#sort carb by ascending and cyl by descending -with sort function in R
mtcars[order(mtcars$carb,-mtcars$cyl),]
mpgcyldisphpdratwtqsecvsamgearcarb
Hornet 4 Drive21.462581103.083.21519.441031
Valiant18.162251052.763.4620.221031
Datsun 71022.84108933.852.3218.611141
Fiat 12832.4478.7664.082.219.471141
Toyota Corolla33.9471.1654.221.83519.91141
Toyota Corona21.54120.1973.72.46520.011031
Fiat X1-927.3479664.081.93518.91141
Hornet Sportabout18.783601753.153.4417.020032
Dodge Challenger15.583181502.763.5216.870032
AMC Javelin15.283041503.153.43517.30032
Pontiac Firebird19.284001753.083.84517.050032
Merc 240D24.44146.7623.693.19201042
Merc 23022.84140.8953.923.1522.91042
Honda Civic30.4475.7524.931.61518.521142
Porsche 914-2264120.3914.432.1416.70152
Lotus Europa30.4495.11133.771.51316.91152
Volvo 142E21.441211094.112.7818.61142
Merc 450SE16.48275.81803.074.0717.40033
Merc 450SL17.38275.81803.073.7317.60033
Merc 450SLC15.28275.81803.073.78180033
Duster 36014.383602453.213.5715.840034
Cadillac Fleetwood10.484722052.935.2517.980034
Lincoln Continental10.4846021535.42417.820034
Chrysler Imperial14.784402303.235.34517.420034
Camaro Z2813.383502453.733.8415.410034
Ford Pantera L15.883512644.223.1714.50154
Mazda RX42161601103.92.6216.460144
Mazda RX4 Wag2161601103.92.87517.020144
Merc 28019.26167.61233.923.4418.31044
Merc 280C17.86167.61233.923.4418.91044
Ferrari Dino19.761451753.622.7715.50156
Maserati Bora1583013353.543.5714.60158

 

How to sort in R – sorting a vector:

A vector in R can be sorted using a simple sort() function as shown below.

# sort function in R: Ascending order
x<-c(3,5,1,2,4,7)
sort(x,decreasing=FALSE)

output: the vector is sorted in ascending order

[1]  1  2  3  4  5  7
# sorting with sort function in R: descending order 
sort(x,decreasing=TRUE)

output: the vector is sorted in descending order

[1]  7  5  4  3  2  1

previous small how to sort in r                                                                                                           next small how to sort in r

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.

    View all posts