# rev R Function | 3 Examples (Reverse of Vector, Data Frame by Column & by Row)

**Basic R Syntax:**

rev(x) |

rev(x)

The rev R function **returns a reversed version of a vector or other data objects**. The basic R code for the rev command is illustrated above.

In this R tutorial, I’m going to show you **three examples** for the usage of rev in R.

Let’s start right away…

## Example 1: Reverse Vector via rev() R Function

Typically, the rev R function is used to reverse the order of vectors. Let’s create an example vector:

vec <- 1:10 # Create example vector vec # Print example vector to RStudio console # 1 2 3 4 5 6 7 8 9 10 |

vec <- 1:10 # Create example vector vec # Print example vector to RStudio console # 1 2 3 4 5 6 7 8 9 10

Our example vector consists of ten numbers ordered from 1 to 10. Now, let’s reverse this vector with the rev() command:

vec_rev <- rev(vec) # Apply rev function to vector vec_rev # Print reversed example vector # 10 9 8 7 6 5 4 3 2 1 |

vec_rev <- rev(vec) # Apply rev function to vector vec_rev # Print reversed example vector # 10 9 8 7 6 5 4 3 2 1

As you can see, we reversed the order of our vector from 10 to 1.

Easy! But stay with me, there is more…

## Example 2: Apply rev to Data Frame to Reverse Columns

With the R rev function, we can also reverse the ordering of the columns of a data frame. Let’s create some example data:

data <- data.frame(x1 = 1:5, # Create example data.frame x2 = 6:10, x3 = 11:15) data # Print example data.frame to RStudio console |

data <- data.frame(x1 = 1:5, # Create example data.frame x2 = 6:10, x3 = 11:15) data # Print example data.frame to RStudio console

**Table 1: Example Data Frame.**

Our dataset has three columns containing the numbers 1 to 5, 6 to 10, and 11 to 15, respectively.

We can reverse the order of the columns with the following R code:

data_rev <- rev(data) # Apply rev function to data.frame data_rev # Print reversed example data.frame |

data_rev <- rev(data) # Apply rev function to data.frame data_rev # Print reversed example data.frame

**Table 2: Example Data Frame Reversed by Column.**

As you can see, the positions of X1 and X3 are exchanged.

OK nice, so can we do that also by row? Yes, of cause we can:

## Example 3: Reverse Order of Rows

For the third example, I’m going to use the same data frame as in Example 2. We can reverse the order of the rows of this data frame with the following lines of code:

data_rev_row_a <- apply(data, 2, rev) # rev() & apply() functions combined data_rev_row_a # Print data.frame _a reversed by row |

data_rev_row_a <- apply(data, 2, rev) # rev() & apply() functions combined data_rev_row_a # Print data.frame _a reversed by row

As you have seen, we used rev in combination with the apply function. You might say that’s too complicated – But no problem, there’s an even simpler solution which relies only on the nrow function:

data_rev_row_b <- data[nrow(data):1, ] # Alternative without rev() data_rev_row_b # Print data.frame _b reversed by row |

data_rev_row_b <- data[nrow(data):1, ] # Alternative without rev() data_rev_row_b # Print data.frame _b reversed by row

**Table 3: Example Data Frame Reversed by Row.**

## Video: rev & Related Functions

Still hungry? No worries, there is more to learn. In the following video tutorial of the LearnR YouTube channel you will see a bunch of further examples that you can use for your analytics in R programming – Not only for rev(), but also for related functions such as which(), order(), attach(), and matrix().

## Further Reading

### Subscribe to my free statistics newsletter:

### R Tutorials

abs Function in R

all & any R Functions

Set Aspect Ratio of Plot

attach & detach R Functions

attr, attributes & structure in R

cbind R Command

Change ggplot2 Legend Title

Character to Numeric in R

Check if Object is Defined

col & row sums, means & medians

Complete Cases in R

Concatenate Vector of Strings

Convert Date to Weekday

cumsum R Function

Data Frame Column to Numeric

diff Command in R

difftime R Function

dim Function in R

dir R Function

Disable Scientific Notation

Draw Segments in R

droplevels R Example

Evaluate an Expression

Extract Characters from String

Factor to Numeric in R

Format Decimal Places

get, get0 & mget in R

is.na R Function

is.null Function in R

jitter R Function

Join Data with dplyr Package

length Function in R

lowess R Smoothing Function

max and min Functions in R

NA Omit in R

nchar R Function

ncol Function in R

nrow Function in R

outer Function in R

pairs & ggpairs Plot

parse, deparse & R expression

paste & paste0 Functions in R

pmax and pmin R Functions

polygon Plots in R

pretty R Function

R Find Missing Values

R Functions List (+ Examples)

R NA – Values

R Replace NA with 0

rbind & rbind.fill in R

Read Excel Files in R

readLines, n.readLines & readline

Remove Element from List

Remove Legend in ggplot2

Rename Column Name in R

Replace Last Comma of String

rev R Command

Round Numeric Data in R

Save & Load RData Workspace

scan R Function

setdiff R Function

setNames vs. setnames in R

sink Command in R

Sort, Order & Rank Data in R

sprintf Function in R

Square Root in R

str_c Function of stringr Package

str_sub Function of stringr Package

strptime & strftime Functions

substr & substring R Commands

sweep R Function

Transform Data Frames

union Function in R

unlist in R

weekdays, months, quarters & julian in R

with & within R Functions

Write Excel File in R