The order() function in R is very useful in sorting a particular data values according to a specific variable.
Hello folks, changing the order of the values or elements present in a data frame or a vector is found to be very effective in data analysis. Hence, this R tutorial is focussed on the order() function which is used to order the data.
Table of Contents
Let’s start with the syntax
Order() = The order() function will arranges the data or orders the data based on given parameters.
- x = data frame or a vector
- decreasing = Orders the data in decreasing fashion, by default it will be FALSE.
- na.last = It moves the NA elements to the last, otherwise, it considers NA at the first as well.
Ordering a simple vector in R
In this section, we can try to order a simple vector having some values in it. Let’s make use of order() function to order the values as well.
This is a simple vector and now we can try to order the values.
#creates a vector x<-c(3.5,7.8,5.6,1.1,2.9,4.4) #orders the values in the vector in an ascending or increasing fashion x[order(x)]
You will get the values arranged in the increasing fashion because by default the parameter is set as decreasing = F.
Output = 1.1 2.9 3.5 4.4 5.6 7.8
The above output shows the ascending order, now let’s order the values in a decreasing fashion.
#creates a vector x<-c(3.5,7.8,5.6,1.1,2.9,4.4) #orders the data in the decreasing fashion x[order(x,decreasing = T)]
Output = 7.8 5.6 4.4 3.5 2.9 1.1
Ascending and descending ordering
With the help of order() function, you can easily order the values in both increasing and decreasing fashion as well. Let’s see how it works.
Let’s create a data frame using ‘tibble’ function.
Tibble: Tibble in R is widely used to generate a data frame with random values. It may include multiple rows and columns as well.
#import the required package library(dplyr) #creates a data frame with normal distribution values. df<-tibble( column1=rnorm(5,5,1.5), column2=rnorm(5,5,1.5), column3=rnorm(5,5,1.5), column4=rnorm(5,5,1.5))
The data frame created using tibble function is shown below.
Let’s order the particular columns in this data frame.
#orders the values in the column 1 df[order(df$column1),]
As you can in the above output, column 1 has been sorted in increasing fashion.
Let’s see how we can order the column in a data frame as well and also using the ‘-‘ (negative) sign to order the values in a decreasing fashion.
#orders the values [decresing the column values] df[order(-df$column1),]
As you can see in the above piece of code column 1 is backed by the negative ‘-‘ sign which indicates the decreasing order. Like this method also you can change the ordering fashion instead of mentioning parameters as shown in the syntax and in the above examples.
Ordering the values in a matrix
It will be exciting to order a matrix, isn’t it? Well, in this section let’s try to create a matrix and after that, we are going to order that matrix using order() function as well.
Let’s see how it works.
#creates a 5x5 matrix having values from 1 to 25 x<-matrix(1:25,5,5) x
We have created a 5×5 matrix, where the values present in the matrix will be 1:25 i.e. numbers from 1 to 25. Now we are all set to order the matrix.
#orders the column1 in the matrix x[order(x[,1],decreasing = T),]
In the above output, you can see that the values present in the matrix were ordered in the decreasing fashion. You can also reorder the matrix in an ascending fashion by mentioning decreasing = F in the above code.
Ordering the values in a data frame
Till now, we have discussed many examples. Now, as a final step, we are going to order a data frame as it is our final motive. Because finally all of us have to deal with a data set. So working or practicing on a data set will be really helpful for all of us.
Let’s see how it works.
First, we are going to import a dataset, where we are applying all our discussed techniques and topics as well.
The below is the data of the ‘airquality’ dataset which is usually available by default in R.
I particularly choose ‘airquality’ dataset, because it includes NA values as well. So let’s apply all our findings in the above sections into this data set and order the values.
We are operating on the ‘Ozone’ column values in the data set.
#orders the values by eliminating NA values in Ozone column x[order(x$Ozone,decreasing = F,na.last = NA),]
As you can see in the above output, all the NA values will be excluded by the na.last = NA command. You can also observe that the values are arranged in increasing fashion as well.
Now, let’s order the values in the decreasing fashion.
x[order(x$Ozone,decreasing = T,na.last = NA),]
In the above output, you can see that the values are arranged in the decreasing fashion with the elimination of the NA values in the Ozone column
Well, in this tutorial we have discussed the order() function and its uses and samples in R. I hope by now you understood the topic well and implement in your analysis as well.
That’s all about the order() function in R. For more updates, stay tuned. For any queries or doubts, don’t hesitate to hit the comment section. Happy ordering!!!
More study: R documentation