Arrange in R – How to Reorganize a Data Frame

When programming for data science and loading in data from outside sources, the values are not necessarily going to be in the order that you need them to be. When you load in a data frame from an outside source, you may need a data manipulation function that will fix this problem for you.

Description

When you do an arrange in r you use the arrange function, which has the format of arrange(data, group) and it arranges the data according to the grouping variable. In this function “data” is the name of the dataframe being arranged and “group” is the column name based on which the data will be arranged in ascending order. The result will be another data frame whose order has been rearranged according to the grouping column name. It is a simple function to use, and one that will come in handy if you need to reorganize a data frame from within your program.

Explanation

The arrange function is the function on which to base r ordering of data frames. This function is used to arrange rows of a data frame in ascending order, as opposed to descending order, based on the column provided to this order function. It does not work on multiple columns. This function is useful when you have a data frame whose data is not in the order that you needed to be. You can select which column to organize the data allowing you to rearrange the data to fit what you are doing and how you want to present the data.

Examples

Here are three examples of the arrange function in action.

> library(dplyr)
> df = data.frame(id=c(111, 122, 143, 154), name=c(‘Tom’,’Sue’,’Bob’,’Mark’), age=c(50, 45, 60, 56))
> df2 = arrange(df, name)
> df2
id name age
1 143 Bob 60
2 154 Mark 56
3 122 Sue 45
4 111 Tom 50

In this example, the arrange function is arranging the data according to the name column. Note that the names are in alphabetical ordering.

> library(dplyr)
> df = data.frame(id=c(111, 122, 143, 154), name=c(‘Tom’,’Sue’,’Bob’,’Mark’), age=c(50, 45, 60, 56))
> df3 = arrange(df, age)
> df3
id name age
1 122 Sue 45
2 111 Tom 50
3 154 Mark 56
4 143 Bob 60

In this example, the arrange function is arranging each data value according to the age column. Note that the ages are in numerical order.

> library(dplyr)
> df = data.frame(id=c(111, 122, 143, 154), name=c(‘Tom’, ‘Sue’, ‘Bob’,’Mark’), age=c(50, 45, NA, 60))
> df4 = arrange(df, age)
> df4
id name age
1 122 Sue 45
2 111 Tom 50
3 154 Mark 60
4 143 Bob NA

In this example, the arrange function is arranging the data in the case where the data frame has missing values. Note that the missing values are placed at the end of the list.

Application

The most common applications of the arrange function are placing names in alphabetical order, and numerical values in numerical order. A good example would be a company rearranging a data frame of employee data into the alphabetical order of their names. Another example would be reordering the same employee data set in order of their salaries. This function can be easily applied to any place where you need to organize your data in alphabetical or numerical ordering. Placing data in alphabetical or numerical ordering can make it easier to find a specific data group that you are looking for.

The arrange function is a handy and easy-to-use function for reordering data frames. It is a useful tool for reordering your data into a more meaningful form. You will find it extremely useful in your r programming.

Scroll to top
Privacy Policy