How to reverse the order of a dataframe in R?
Last Updated :
23 Dec, 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
data_frame <- data.frame (col1 = c (1:4),
col2 = letters [1:4],
col3 = c (8:11))
print ( "Original Dataframe" )
print (data_frame)
rev_data_frame <- rev (data_frame)
print ( "Modified Dataframe" )
print (rev_data_frame)
|
Output
[1] "Original Dataframe"
col1 col2 col3
1 1 a 8
2 2 b 9
3 3 c 10
4 4 d 11
[1] "Modified Dataframe"
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
data_frame <- data.frame (col1 = c (1:4),
col2 = letters [1:4],
col3 = c (8:11))
print ( "Original Dataframe" )
print (data_frame)
rev_data_frame <- data_frame[, ncol (data_frame):1 ]
print ( "Modified Dataframe" )
print (rev_data_frame)
|
Output
[1] "Original Dataframe"
col1 col2 col3
1 1 a 8
2 2 b 9
3 3 c 10
4 4 d 11
[1] "Modified Dataframe"
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
data_frame <- data.frame (col1 = c (1:4),
col2 = letters [1:4],
col3 = c (8:11))
print ( "Original Dataframe" )
print (data_frame)
transpose <- t (data_frame)
transpose <- as.data.frame (transpose)
rev_data_frame <- rev (transpose)
rev_data_frame <- t (rev_data_frame)
rev_data_frame <- as.data.frame (rev_data_frame)
print ( "Modified Dataframe" )
print (rev_data_frame)
|
Output
[1] "Original Dataframe"
col1 col2 col3
1 1 a 8
2 2 b 9
3 3 c 10
4 4 d 11
[1] "Modified Dataframe"
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
data_frame <- data.frame (col1 = c (1:4),
col2 = letters [1:4],
col3 = c (8:11))
print ( "Original Dataframe" )
print (data_frame)
rev_data_frame <- apply (data_frame, 2, rev)
rev_data_frame <- as.data.frame (rev_data_frame)
print ( "Modified Dataframe" )
print (rev_data_frame)
|
Output
[1] "Original Dataframe"
col1 col2 col3
1 1 a 8
2 2 b 9
3 3 c 10
4 4 d 11
[1] "Modified Dataframe"
col1 col2 col3
1 4 d 11
2 3 c 10
3 2 b 9
4 1 a 8
Similar Reads
How to reverse the order of given vector in R?
In this article, we will discuss how to reverse the order of elements in a vector in R Programming Language. It can be done using the rev() function. It returns the reverse version of data objects. Syntax: rev(x) Parameter: x: Data object Returns: Reverse of the data object passed Example 1: Here we
2 min read
How to Reverse the order of given vector in R ?
In this article, we will discuss how to reverse the order of elements in a vector using rev() function in R Programming Language. Syntax: rev(vector_name) Approach create a vectorreverse the elements Syntax to create a vector: c(value1,value2,...,valuen) Example: Creating the vector [GFGTABS] R # cr
1 min read
How to Remove Rows in R DataFrame?
In this article, we will discuss how to remove rows from dataframe in the R programming language. Method 1: Remove Rows by Number By using a particular row index number we can remove the rows. Syntax: data[-c(row_number), ] where. data is the input dataframerow_number is the row index position Exam
2 min read
How to Sort a DataFrame in R ?
In this article, we will discuss how to sort the dataframe in R Programming Language. In R DataFrame is a two-dimensional tabular data structure that consists of rows and columns. Sorting a DataFrame allows us to reorder the rows based on the values in one or more columns. This can be useful for var
5 min read
How to get the structure of a given DataFrame in R?
In this article, we will see how to get the structure of a DataFrame in R programming. Structure of a dataframe generally means the inner details about the dataframe. Steps for Getting Structure of DataFrame: Create dataframe.The size of each vector should be the same.Follow the syntax while creatin
2 min read
How to shuffle a dataframe in R by rows
In this article, we will discuss how to shuffle a dataframe by rows in the R programming language. Shuffling means reordering or rearranging the data. We can shuffle the rows in the dataframe by using sample() function. By providing indexing to the dataframe the required task can be easily achieved.
2 min read
How to Retrieve Row Numbers in R DataFrame?
In this article, we will discuss how to Retrieve Row Numbers in R Programming Language. The dataframe column can be referenced using the $ symbol, which finds its usage as data-frame$col-name. The which() method is then used to retrieve the row number corresponding to the true condition of the speci
2 min read
How to remove a subset from a DataFrame in R ?
A subset is a combination of cells that form a smaller data frame formed out from the original data frame. A set of rows and columns can be removed from the original data frame to reduce a part of the data frame. The subset removal can be based on constraints to which rows and columns are subjected
4 min read
How to split DataFrame in R
In this article, we will discuss how to split the dataframe in R programming language. A subset can be split both continuously as well as randomly based on rows and columns. The rows and columns of the dataframe can be referenced using the indexes as well as names. Multiple rows and columns can be r
4 min read
How To Merge Two DataFrames in R ?
In this article, We are going to see how to merge two R dataFrames. Merging of Data frames in R can be done in two ways. Merging columnsMerging rowsMerging columns In this way, we merge the database horizontally. We use the merge function to merge two frames by one or more common key variables(i.e.,
2 min read