Reorder or Rearrange the column of dataframe in R

Reorder or Rearrange the column of the dataframe in R, is accomplished either by column name or by column position. Re ordering by column name and column position are two prominent ways of rearranging the columns in R. Other ways we could think of  are Rearranging column in alphabetical order. Shift/Move a column to First position or Last Position etc. 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 with an example for each. Let’s see how to

  • Rearrange the column of the dataframe by column name.
  • Rearrange the column of the dataframe by column position.
  • Rearrange or Reorder the column of the dataframe in R using Dplyr with select function.
  • Rearrange or Reorder the column name alphabetically
  • Rearrange or Reorder the column name in alphabetically reverse order
  • move or shift a column to First position in R.

Rearrange column in R 11

Let’s First create a dataframe

df1 = data.frame( Name = c('George','Andrea', 'Micheal','Maggie','Ravi','Xien','Jalpa'), 
                  Grade_score=c(4,6,2,9,5,7,8),
                  Mathematics1_score=c(45,78,44,89,66,49,72),
                  Science_score=c(56,52,45,88,33,90,47))

df1 will be

Re order or Rearrange the column of dataframe in R 1

 

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

Re order or Rearrange the column of dataframe in R 2

 

 

Rearrange the column of the dataframe by column name:

In the below we rearrange the column by column name

# Re order the column by name

df3 = df1[,c("Grade_score","Mathematics1_score","Name","Science_score")]
df3

so the reordered dataframe will be

Re order or Rearrange the column of dataframe in R 3

 

 

Rearrange or Reorder the column of the dataframe using Dplyr :

Re order the column using select function with all the columns arranged in order of our choice. select() function in dplyr helps us to select the column in the order which we want.

# Reorder the columns of the dataframe


library(dplyr)

Mydata1 = select(df1,Grade_score,Science_score,Mathematics1_score,Name) 
Mydata1 

so the resultant dataframe with all the column names rearranged using select() function will be

Reorder or Rearrange the column of dataframe in R 3

 

 


Rearrange or reorder the column Alphabetically in R:

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)
df1 %>% select(order(colnames(df1)))

OR

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

df1[,order(colnames(df1))]

so the resultant dataframe with columns ordered alphabetically will be

Rearrange column in R 13

 

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)
df1 %>% select(order(colnames(df1),decreasing = TRUE))

OR

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

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

so the resultant dataframe with columns ordered alphabetically will be

Rearrange column in R 14

 

 


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 = df1 %>% select(Grade_score, everything())
new_df

so the resultant table will have Grade_Score as first column

Rearrange column in R 16

 


Other Related Topics :

 

                                                                                                         

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.