R – Reorder the Columns in the Data Frame


Consider a requirement that you need to reorder the columns in an R data frame. How can you do that? Go through this article to get the solution for the given requirement.

Now, we will see how to reorder the columns in the data frame. First, let’s create a data frame.

#create a dataframe-market that has 4 rows and 5 columns.

market=data.frame(market_id=c(1,2,3,4),market_name=c(‘M1’,‘M2’,‘M3’,‘M4’),
market_place=c(‘India’,‘USA’,‘India’,‘Australia’),market_type=c(‘grocery’,‘bar’,‘grocery’,
‘restaurent’),market_squarefeet=c(120,342,220,110))

#display the market dataframe

print(market)

Result:

You can see the market data frame here:

Let’s discuss them one by one.

Method 1: select() With Column Names

The select() method available in the dplyr library is used to select the columns provided in the order inside this method.

It takes two parameters. The first parameter represents the DataFrame object, and the second parameter represents the column names.

Syntax:

select(dataframe_object,column,…………)

Parameters:

  1. The dataframe_object is the data frame.
  2. The column represents the column names in which the data frame is ordered based on these columns.

Example

In this example, we will reorder the columns in the market-dataframe: market_name, market_place, market_squarefeet, and market_id,market_type.

library(dplyr)

#create a dataframe-market that has 4 rows and 5 columns.

market=data.frame(market_id=c(1,2,3,4),market_name=c(‘M1’,‘M2’,‘M3’,‘M4’),
market_place=c(‘India’,‘USA’,‘India’,‘Australia’),market_type=c(‘grocery’,‘bar’,‘grocery’,
‘restaurent’),market_squarefeet=c(120,342,220,110))

#reorder the market-dataframe – market_name,market_place,market_squarefeet, market_id and market_type

print(select(market,market_name,market_place,market_squarefeet, market_id,market_type))

Result:

From the previous result, we can see that the data frame is returned with respect to the columns provided.

Method 2: select() With Column Indices

The select() method available in the dplyr library is used to select the columns provided in the order inside this method.

It takes two parameters. The first parameter represents the DataFrame object, and the second parameter represents the column indices.

Syntax:

select(dataframe_object,column,…………)

Parameters:

  1. The dataframe_object is the data frame.
  2. The column represents the column indices in which the data frame is ordered based on these columns.

Example

In this example, we will reorder the columns in the market-dataframe: 2, 3, 5, 1, and 4.

library(dplyr)

#create a dataframe-market that has 4 rows and 5 columns.

market=data.frame(market_id=c(1,2,3,4),market_name=c(‘M1’,‘M2’,‘M3’,‘M4’),
market_place=c(‘India’,‘USA’,‘India’,‘Australia’),market_type=c(‘grocery’,‘bar’,‘grocery’,
‘restaurent’),market_squarefeet=c(120,342,220,110))

#reorder the market-dataframe – market_name,market_place,market_squarefeet, market_id and market_type

print(select(market,2,3,5,1,4))

Result:

From the previous result, we can see that the data frame is returned with respect to the column indices provided.

Method 3: select() With order()

The select() method takes the order() method as a parameter to reorder the data frame in ascending or descending order. It takes two parameters. The first parameter takes the order() method and the second parameter decreases, which takes Boolean values. FALSE specifies reordering the data frame based on the column names in ascending order, and TRUE specifies reordering the data frame based on the column names in descending order. Finally, the select() method will load this into the DataFrame object using the %>% operator.

Syntax:

dataframe_object %>% select(order(colnames(dataframe_object ),decreasing))

Parameters:

  1. The colnames(dataframe_object) return the columns and load into order() method.
  2. Decreasing is used to reorder the data frame in ascending or descending order.

Example 1

In this example, we will reorder the columns in the market-dataframe in ascending order.

#create a dataframe-market that has 4 rows and 5 columns.

market=data.frame(market_id=c(1,2,3,4),market_name=c(‘M1’,‘M2’,‘M3’,‘M4’),
market_place=c(‘India’,‘USA’,‘India’,‘Australia’),market_type=c(‘grocery’,‘bar’,‘grocery’,
‘restaurent’),market_squarefeet=c(120,342,220,110))

#reorder the market-dataframe alphabetically in ascending order

print(market %>% select(order(colnames(market),decreasing = FALSE)))

Result:

From the previous result, we can see that the data frame is reordered with respect to the column names in ascending order.

Example 2

In this example, we will reorder the columns in the market-dataframe by descending order.

#create a dataframe-market that has 4 rows and 5 columns.

market=data.frame(market_id=c(1,2,3,4),market_name=c(‘M1’,‘M2’,‘M3’,‘M4’),
market_place=c(‘India’,‘USA’,‘India’,‘Australia’),market_type=c(‘grocery’,‘bar’,‘grocery’,
‘restaurent’),market_squarefeet=c(120,342,220,110))

#reorder the market-dataframe alphabetically in descending order

print(market %>% select(order(colnames(market),decreasing = TRUE)))

Result:

From the previous result, we can see that the data frame is reordered with respect to the column names in descending order.

Method 4: arrange()

The arrange() method in the dplyr library is used to arrange the data frame based on the column in ascending order. It will load the arranged data frame into the data frame using the %>% operator. It is also possible to arrange the data frame in descending order by specifying the desc() method.

Based on the values in a specified column, it will reorder the columns.

Syntax for ascending order:

dataframe_object %>% arrange(column)

Syntax for descending order:

dataframe_object %>% arrange(desc(column))

Parameter:

It takes only one parameter, i.e., a column in which the remaining columns are reordered based on these column values.

Example 1

In this example, we will reorder the columns in the data frame based on market_place column values in ascending order.

library(dplyr)

#create a dataframe-market that has 4 rows and 5 columns.

market=data.frame(market_id=c(1,2,3,4),market_name=c(‘M1’,‘M2’,‘M3’,‘M4’),
market_place=c(‘India’,‘USA’,‘India’,‘Australia’),market_type=c(‘grocery’,‘bar’,‘grocery’,
‘restaurent’),market_squarefeet=c(120,342,220,110))

#reorder the market-dataframe in ascending order based on market_place

print(market %>% arrange(market_place))

Result:

Here, the remaining columns are reordered based on market_place column values in ascending order.

Example 2

In this example, we will reorder the columns in the data frame based on market_place column values in descending order.

library(dplyr)

#create a dataframe-market that has 4 rows and 5 columns.

market=data.frame(market_id=c(1,2,3,4),market_name=c(‘M1’,‘M2’,‘M3’,‘M4’),
market_place=c(‘India’,‘USA’,‘India’,‘Australia’),market_type=c(‘grocery’,‘bar’,‘grocery’,
‘restaurent’),market_squarefeet=c(120,342,220,110))

#reorder the market-dataframe in descending order based on market_place

print(market %>% arrange(desc(market_place)))

Result:

We can see remaining columns are reordered based on market_place column values in descending order.

Method 5: arrange_all()

The arrangeall() method in the dplyr library is used to arrange the data frame based on column names in ascending order.

Syntax:

arrange_all(dataframe_object)

Parameter:

It takes only one parameter, i.e., the DataFrame object.

Example

In this example, we will reorder the columns in the data frame using the arrange_all() method.

library(dplyr)

#create a dataframe-market that has 4 rows and 5 columns.

market=data.frame(market_id=c(1,2,3,4),market_name=c(‘M1’,‘M2’,‘M3’,‘M4’),
market_place=c(‘India’,‘USA’,‘India’,‘Australia’),market_type=c(‘grocery’,‘bar’,‘grocery’,
‘restaurent’),market_squarefeet=c(120,342,220,110))

#reorder the market-dataframe alphabetically

print(arrange_all(market))

Result:

We can see that the data frame is reordered based on column names in ascending order.

Conclusion

In the article, we have seen five different methods to reorder the columns in the data frame. The select() method is used to reorder the data frame columns using column names and column indices. Next, we used order() with select(), and we saw how to reorder the columns based on the column values in both increasing and decreasing order using the arrange() method. Finally, we used arrangeall() to reorder the columns in the data frame based on column names alphabetically.



Source link