How to reverse the elements of a vector object in R

Overview

In R, the rev() function is used to reverse the elements of a vector object.

Syntax

rev(x)
Syntax for the rev() function in R

Parameters

This function takes a single parameter value, x, representing the vector object of which the elements are to be reversed.

Return value

This function returns a vector object with its elements reversed.

Example

# A code to illustrate the rev() function in R
# Creating a vector objectd
a <- c(1, 5, 6, 7, 33, 2, 9)
b <- c(1:10)
# reversing the elements of the vector
rev(a)
rev(b)

Explanation

  • Line 3–4: We create vector objects, a and b.
  • Line 8–9: We call the rev() function to reverse the elements in a and b.

Free Resources