Rearrange or Reorder the rows and columns in R using Dplyr

In Order to Rearrange or Reorder the column of dataframe in R using Dplyr we use select() function. Dplyr package in R is provided with select() function which re orders the columns. In order to Rearrange or Reorder the rows of the dataframe in R using Dplyr we use  arrange() funtion. The arrange() function is used to rearrange rows in ascending or descending order. Let’s see an example of each

  • Rearrange or Reorder the column of the dataframe in R using Dplyr
  • Reorder or Rearrange the rows of dataframe in Descending order using R dplyr
  • Reorder or Rearrange the rows of dataframe in Ascending order using R dplyr

We will be using student_df dataframe to depict the reorder of variable.Rearrange or Reorder the rows and columns in R using Dplyr 1

 

 

Rearrange or Reorder the column of the dataframe in R using Dplyr :

Re order the column using select function with all the columns arranged in order of our choice.

# Reorder the columns of the dataframe


library(dplyr)

Mydata1 = select(student_df,Science_score,Language_score,Mathematics_score,Name)
Mydata1 

so the resultant dataframe with all the column names rearranged will be

Rearrange or Reorder the rows and columns in R using Dplyr 2

 

 

Rearrange or Reorder specific columns of the dataframe in R using Dplyr :

Re order only with name, mathematics_score and science_score columns

# Reorder the columns of the dataframe
library(dplyr)


Mydata1 = select(student_df,Science_score,Mathematics_score,Name)
Mydata1 

so the resultant dataframe with specific column names rearranged will be

Rearrange or Reorder the rows and columns in R using Dplyr 3

 

 

Reorder or Rearrange the rows of the dataframe in ascending order using dplyr

Re order rows of the dataframe is accomplished using arrange() Function as shown below. arrange() Function rearranges the rows of the dataframe in ascending order as shown below

# Reorder the rows of the dataframe in ascending order

library(dplyr)

student_df %>% arrange(Name)

So we have arranged the rows in ascending order of Name so the resultant dataframe will be
Rearrange or Reorder the rows and columns in R using Dplyr 4

 

 

Reorder or Rearrange the rows of the dataframe in descending order using dplyr

Re order rows of the dataframe is accomplished using arrange() Function as shown below. arrange() Function along with desc()  rearranges the rows of the dataframe in descending order as shown below

# Reorder the rows of the dataframe in descending order

library(dplyr)
student_df %>% arrange(desc(Name))

So we have arranged the rows in descending order of Name so the resultant dataframe will be
Rearrange or Reorder the rows and columns in R using Dplyr 5

 

 

Re arrange or Re order the column of dataframe in R using Dplyr                                                                                                          Re arrange or Re order the column of dataframe in R using Dplyr next