Reorder the column of dataframe in R using Dplyr

02 Sep, 2021
In this article, we will discuss how to rearrange or reorder the column of the dataframe using dplyr package in R Programming Language.
Creating Dataframe for demonstration:
R
# load the packagelibrary(dplyr)  # create the dataframe with three columns# id , department and salary with 8 rowsdata = data.frame(id = c(7058, 7059, 7060, 7089,                         7072, 7078, 7093, 7034),                                      department = c('IT','sales','finance',                                 'IT','finance','sales',                                 'HR','HR'),                                      salary = c(34500.00, 560890.78, 67000.78,                              25000.00, 78900.00, 25000.00,                             45000.00, 90000))  # display dataframedata
Output:
We are going to use a select() method to reorder columns.
Syntax: select(dataframe,columns)
where
dataframe is the input dataframe
columns are the input columns to be reordered
Here we are rearranging the dataframe(id,department,salary) to (salary,id,department)
R
print("Before: ")data  print("After: ")  # reorder the columns using selectselect(data, salary, id, department)
Output:
Here, we will rearrange the columns using the index/position of the column. So we will use select method to do this.
Note: index/position  of the column starts with 1
Syntax: select(dataframe.index_positions)
Where,
dataframe is the input dataframe
index_positions are column positions to be rearranged
Here we are rearranging to different positions.
R
# display actual  dataframeprint("actual  dataframe")print(data)  print("reorder the column with position")  # reorder the columns with column positions# using selectprint(select(data,3,1,2))
Output:
Here we are using order() function along with select() function to rearrange the columns in alphabetical order. So we will order the columns using colnames function.
Syntax: dataframe %>% select(order(colnames(dataframe)))
where,
dataframe is the input dataframe
%>% is the pipe operator to pass the result to the dataframe
order() is used to rearrange the dataframe columns in alphabetical order
colnames() is the function to get the columns in the dataframe
Here we are rearranging the data based on column names in alphabetical order.
R
print("Actual dataframe")  # display actual  dataframeprint(data)  print("Reorder dataframe")  # rearrange the columns in alphabetic # orderdata %>% select(order(colnames(data)))
Output:
so we will order the columns using colnames function in reverse.
Syntax: dataframe %>% select(order(colnames(dataframe),decreasing=TRUE))
where,
dataframe is the input dataframe
%>% is the pipe operator to pass the result to the dataframe
order() is used to rearrange the dataframe columns in alphabetical order
colnames() is the function to get the columns in the dataframe
decreasing=TRUE parameter specifies to sort the dataframe in descending order
Here we are rearranging the data based on column names in alphabetical order in reverse.
R
print("Actual dataframe")  # display actual  dataframeprint(data)  print("Reorder dataframe")  # rearrange the columns in reverse alphabetic orderdata %>% select(order(colnames(data),                      decreasing = TRUE))
Output:
We are going to use everything() method to shift the column to first, so in this way, we can rearrange the dataframe.
Syntax: dataframe %>% select(column_name, everything())
where,
dataframe is the input dataframe
column_name is the column to be shifted first
R program to shift the department column as first
R
print("Actual dataframe")  # display actual  dataframeprint(data)  print("Reorder dataframe")  # getting department column as firstdata %>% select(department, everything())
Output:
Here we are going to rearrange the rows based on a particular column in ascending order using arrange() function
Syntax: dataframe %>% arrange(column_name)
Where
dataframe is the input dataframe
column_name is the column in which dataframe rows are arranged based on this column
R program to rearrange rows based on department column
R
print("Actual dataframe")  # display actual  dataframeprint(data)  print("Reorder dataframe")  # arrange the rows based on department columndata %>% arrange(department)
Output:
Here we are going to rearrange the rows based on a particular column in ascending order using arrange() function along with desc() function.
Syntax: dataframe %>% arrange(desc(column_name))
Where
dataframe is the input dataframe
column_name is the column in which dataframe rows are arranged based on this column in descending order
R
print("Actual dataframe")  # display actual  dataframeprint(data)  print("Reorder dataframe")  # arrange the rows based on salary# column in descending orderdata %>% arrange(desc(salary))
Output:
Here we are going to arrange/ reorder the rows based on multiple variables in the dataframe, so we are using arrange_all() function
Syntax: arrange_all(dataframe)
R
print("Actual dataframe")  # display actual  dataframedata  print("Reorder dataframe")  # rearrange multiple columnsarrange_all(data)
Output:
Picked
R Dplyr
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Change Color of Bars in Barchart using ggplot2 in R
How to Split Column Into Multiple Columns in R DataFrame?
Group by function in R using Dplyr
How to Change Axis Scales in R Plots?
How to filter R DataFrame by values in a column?
R - if statement
Logistic Regression in R Programming
Replace Specific Characters in String in R
How to import an Excel File into R ?
Joining of Dataframes in R Programming