R dataframe reorder columns

Reorder Columns in R Dataframe (Step by Step)

A dataframe in R is a two-dimensional data structure used to store the data in rows and columns. In this tutorial, we will look at how to reorder the columns of a dataframe in R with the help of some examples.

How do you reorder columns of an R dataframe?

The select() function is generally used to select one or more columns from a dataframe in R but you can also use it to reorder the columns.

The following is the syntax –

select(dataframe_input, column_names)

To reorder the columns, pass the dataframe as the first argument and the column names in the order that you want as the subsequent arguments to the select() function.

It returns an R dataframe with the selected columns in the given order.

Steps to reorder columns of a dataframe in R using select() function

Let’s now look at a step-by-step example of reordering columns of a dataframe in R.

Step 1 – Create a dataframe

First, we will create a dataframe that we will be using throughout this tutorial.

# create a dataframe
employees_df = data.frame(
  "Name"= c("Jim", "Dwight", "Angela", "Tobi", "Kevin"),
  "Age"= c(26, 28, 29, 32, 30),
  "Department"= c("Sales", "Sales", "Accounting", "HR", "Accounting")
# display the dataframe


📚 Data Science Programs By Skill Level


Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

    Name Age Department
1    Jim  26      Sales
2 Dwight  28      Sales
3 Angela  29 Accounting
4   Tobi  32         HR
5  Kevin  30 Accounting

We now have a dataframe containing information about some employees working in an office. The dataframe has columns “Name”, “Age”, and “Department”.

Step 2 – Import the dplyr package

The select() function is defined in the dplyr package which has to be imported before we can actually use it.

# import dplyr package

Here we use the library() function to import the dplyr package.

Step 3 – Select the column using select() function

Let’s say you want the dataframe to have the column order – “Name”, “Department”, and “Age”.

We’ll use the select() function from the dplyr package imported above and pass the column names in the order that we want.

# change column order to - "Name", "Department", "Age"
new_df = select(employees_df, "Name", "Department", "Age")
# display the dataframe


    Name Department Age
1    Jim      Sales  26
2 Dwight      Sales  28
3 Angela Accounting  29
4   Tobi         HR  32
5  Kevin Accounting  30

We get a dataframe with the desired column order.

Note that you can also use the column indices in the select() function (Rows and columns are indexed starting from 1 in R dataframes).

# change column order to - "Name", "Department", "Age" using thier indices
new_df = select(employees_df, 1, 3, 2)
# display the dataframe


    Name Department Age
1    Jim      Sales  26
2 Dwight      Sales  28
3 Angela Accounting  29
4   Tobi         HR  32
5  Kevin Accounting  30

We get the same result as above.

You might also be interested in –

Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top