Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

functions
r
communitycreator

How to reverse the elements of a vector object in R

Onyejiaku Theophilus Chidalu

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)
Implementing the rev() function in R

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.

RELATED TAGS

functions
r
communitycreator

CONTRIBUTOR

Onyejiaku Theophilus Chidalu
RELATED COURSES

View all Courses

Keep Exploring