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 reorders 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. Moving a column to First position or Last Position in R can also accomplished. We thought through the different scenarios of such kind and formulated this post. We will depict multiple scenarios on how to rearrange the column in R.  Let’s see an example of each

  • Rearrange or Reorder the column of the dataframe in R using Dplyr
  • Rearrange the column of the dataframe by column name.
  • Rearrange the column of the dataframe by column position.
  • Rearrange or Reorder the column name alphabetically
  • Rearrange or Reorder the column name in alphabetically reverse order
  • Move or shift the column to the First position/ last position in R.
  • Reorder or Rearrange the rows of dataframe in Descending order using R dplyr using arrange() function
  • Reorder or Rearrange the rows of dataframe in Ascending order using R dplyr using arrange() function
  • Reorder or Rearrange  rows by all variable in R using arrange_all() function

Rearrange or reorder column and rows in R dplyr 1

Lets look how to Rearrange or Reorder the rows and columns in R using Dplyr. Let’s First create a dataframe

#### Create dataframe

student_df = data.frame( Name = c('George','Andrea', 'Micheal','Maggie','Ravi','Xien','Jalpa'), 
                         Mathematics_score=c(62,47,55,74,32,77,86),
                         Language_score=c(45,78,44,89,66,49,72),
                         Science_score=c(56,52,45,88,33,90,47))

student_df

dataframe “student_df”  will be

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

 

 


Rearrange or reorder the column Alphabetically in R:

Rearrange or reorder column and rows in R dplyr 2

Rearranging the column in alphabetical order can be done with the help of select() function & order() function along with pipe operator.  In another method it can also be accomplished simply with help of order() function only. Both the examples are shown below.

#### Reorder the columns of the dataframe in Alphabetical order

library(dplyr)
student_df %>% select(order(colnames(student_df)))

OR

#### Reorder the columns of the dataframe in Alphabetical order

student_df[,order(colnames(student_df))]

so the resultant dataframe with columns ordered alphabetically will be

Rearrange or reorder column and rows in R dplyr 5

 

 

 

Rearrange the column Alphabetically in reverse order:

Rearranging the column in alphabetically reverse order can be done with the help of select() function & order() function along with pipe operator.  In another method it can also be accomplished simply with help of order() function only. Both the examples are shown below.  order() function takes up the parameter decreasing = TRUE, there by reverses the alphabetical order of the columns.

#### Reorder the columns of the dataframe in Alphabetical order

library(dplyr)
student_df %>% select(order(colnames(student_df),decreasing = TRUE))

OR

#### Reorder the columns of the dataframe in Alphabetical order

student_df[,order(colnames(student_df),decreasing = TRUE)]

so the resultant dataframe with columns ordered alphabetically will be

Rearrange or reorder column and rows in R dplyr 6

 

 


Move or Shift a column to First position in R:

A simple method to move a column towards first position is to use select() function along with everything() function. select() function selects the Grade_Score first followed by everything() i.e. every other columns,

#### Move a column to first position

library(dplyr)

new_df = student_df %>% select(Mathematics_score, everything()) 
new_df

so the resultant table will have Grade_Score as first column

 

 

Rearrange the column of the dataframe by column position:

In the below example 2nd ,4th 3rd and 1st column takes the position of 1 to 4 respectively

# Re order the column by position

df2 = df1[,c(2,4,3,1)] 
df2

so the re ordered dataframe will be

Rearrange or reorder column and rows in R dplyr 21

 


Rearrange or reorder the Rows in R

1) Reorder or Rearrange the rows of the dataframe in ascending order using dplyr arrange()

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

 

2) Reorder or Rearrange the rows of the dataframe in descending order using dplyr arrange()

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

 

3) Rearrange rows by multiple variable using arrange_all() function in R dplyr

Lets look at a quick example. First lets create a dataframe

# create a dataframe

library(dplyr)
df <- data.frame(ids=c(2,2,3,1,1),x=c(1,1,2,5,4),y=letters[1:5])

the dataframe will be

arrange_all with dplyr in R 1

arrange_all() function in dplyr package is used to arrange rows by multiple variable in the below example we have rearranged rows by all the three variables

#### arrange all of the dataframe
library(dplyr)
arrange_all(df)

the dataframe will be first sorted or arranged by column “id” and then by column “x” and then by column “y”. so the resultant dataframe will be

arrange_all with dplyr in R 2

For Further understanding on how to Rearrange or Reorder the rows and columns in R using Dplyr one can refer dplyr documentation


Other Related Topics :

 

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

DataScience Made Simple