Related Articles

# How to reverse the order of a dataframe in R?

• Last Updated : 23 May, 2021

Order reversal of the dataframe involves swapping of the rows or columns in such a way, that the elements are accessed from backward. In this article, we will discuss various ways of reversing the order of a dataframe both column and row-wise.

### Reversing the order of columns

Method 1: Using the rev method

The rev() method in R is used to return the reversed order of the R object, be it dataframe or a vector. It computes the reverse columns by default. The resultant dataframe returns the last column first followed by the previous columns. The ordering of the rows remains unmodified. The result has to be stored in a new variable in order to retain the modifications.

Syntax:

rev (df)

Where df is the dataframe.

Example:

## R

 `# declaring a dataframe in R``data_frame <- ``data.frame``(col1 = ``c``(1:4), ``                         ``col2 = ``letters``[1:4],``                         ``col3 = ``c``(8:11))`` ` `print ``(``"Original DataDrame"``)``print ``(data_frame)`` ` `# calculating reverse of dataframe``rev_data_frame <- ``rev``(data_frame)`` ` `print ``(``"Modified DataDrame"``)``print ``(rev_data_frame)`

Output

```[1] "Original DataDrame"
col1 col2 col3
1    1    a    8
2    2    b    9
3    3    c   10
4    4    d   11
col3 col2 col1
1    8    a    1
2    9    b    2
3   10    c    3
4   11    d    4```

Method 2: Using ncol method

ncol() method in R is used to return the number of columns of the specified dataframe. The dataframe is accessed from the last column index to the first column index range, that is 1.

Example:

## R

 `# declaring a dataframe in R``data_frame <- ``data.frame``(col1 = ``c``(1:4), ``                         ``col2 = ``letters``[1:4],``                         ``col3 = ``c``(8:11))`` ` `print ``(``"Original DataDrame"``)``print ``(data_frame)`` ` `# computing reverse``rev_data_frame <- data_frame[,``ncol``(data_frame):1 ] `` ` `print ``(``"Modified DataDrame"``)``print ``(rev_data_frame)`

Output

```[1] "Original DataDrame"
col1 col2 col3
1    1    a    8
2    2    b    9
3    3    c   10
4    4    d   11
col3 col2 col1
1    8    a    1
2    9    b    2
3   10    c    3
4   11    d    4```

### Reversing the order of rows

Method 1: Using transpose and rev methods

The rev() method reverses the order of the matrix on the basis of columns. The idea here lays its approach towards the foundational concepts that transpose of a transposed matrix is equivalent to the original matrix.

Syntax:

t( t( M) ) = M

Where M is the matrix or dataframe.

The transpose of the dataframe is taken, which interchanges the order of rows and columns of the dataframe. Now, the rows become the columns and columns become rows respectively. The resultant transposed matrix is converted to a dataframe using the as.data.frame() method. The rev() method is applied over this transposed dataframe. This reverses the order of rows in the dataframe. The reversed dataframe needs to be transposed again in order to display the dataframe properly. Hence, the dataframe is returned to the original format. The explicit conversion of this output to a dataframe using as.data.frame() is then made.

Example:

## R

 `# declaring a dataframe in R``data_frame <- ``data.frame``(col1 = ``c``(1:4), ``                         ``col2 = ``letters``[1:4],``                         ``col3 = ``c``(8:11))`` ` `print ``(``"Original DataDrame"``)``print ``(data_frame)`` ` `# transpose of dataframe ``transpose <- ``t``(data_frame)`` ` `# converting the result to dataframe``transpose <- ``as.data.frame``(transpose)`` ` `# calculating reverse of dataframe``rev_data_frame <- ``rev``(transpose)`` ` `# transpose of reverse dataframe ``rev_data_frame <- ``t``(rev_data_frame)`` ` `# converting the result to dataframe``rev_data_frame <- ``as.data.frame``(rev_data_frame)`` ` `print ``(``"Modified DataDrame"``)``print ``(rev_data_frame)`

Output

```[1] "Original DataDrame"
col1 col2 col3
1    1    a    8
2    2    b    9
3    3    c   10
4    4    d   11
col1 col2 col3
V4    4    d   11
V3    3    c   10
V2    2    b    9
V1    1    a    8```

Method 2: Using apply method

apply() method in R is used to apply a specified function over the R object, vector, dataframe, or matrix. This method returns a vector or array or list of values obtained by applying the function to the corresponding of an array or matrix.

Syntax:

apply(df , axis, FUN, …)

Parameter :

df – A dataframe or matrix

axis – The axis over which to apply the function. For a dataframe, 1 indicates rows, 2 indicates columns and c(1, 2) indicates rows and columns.

FUN – The function to be applied.

In this approach, the rev function computes the reverse of the dataframe across the rows’ axis.

Example:

## R

 `# declaring a dataframe in R``data_frame <- ``data.frame``(col1 = ``c``(1:4), ``                         ``col2 = ``letters``[1:4],``                         ``col3 = ``c``(8:11))`` ` `print ``(``"Original DataDrame"``)``print ``(data_frame)`` ` `# calculating reverse``rev_data_frame <- ``apply``(data_frame, 2, rev)`` ` `# converting the result to dataframe``rev_data_frame <- ``as.data.frame``(rev_data_frame)`` ` `print ``(``"Modified DataDrame"``)``print ``(rev_data_frame)`

Output

```[1] "Original DataDrame"
col1 col2 col3
1    1    a    8
2    2    b    9
3    3    c   10
4    4    d   11