Reverse the order of dataframe row wise

Inverting the row order of the dataframe is accomplished by order function. Let’s see how to reverse the order of the dataframe by row wise with an example.

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

Reverse the order of dataframe row wise 1

 

Invert the row order in R – Reverse the dataframe order row wise

Inverting the row order in R is done using order() and nrow() function as shown below.

## invert the row order in R

df2=df1[order(nrow(df1):1),]
df2

so the resultant dataframe will be in inverted order

Reverse the order of dataframe row wise 2

 

                                                                                                         

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.

    View all posts