R Program to reverse the order of given vector


February 8, 2023, Learn eTutorial
1775

How to print in the reverse order of a given vector using R programming?

Here we explain how to write an R program to display the vector elements in the reverse order. Here we are using a built-in function rev(). The rev() function helps to give a reversed version of its argument. The syntax of rev() function is like

rev(x)

Where,

  • x is a vector or object which needs to be reversed

In this R program, we are displaying the original values of the vector first. Then, we directly give the values to built-in functions and print the function result. Here we used variable A for assigning vector values and empty vector Rev for assigning reversed vector.

ALGORITHM

STEP 1: Assign variable A with vector values

STEP 2: Display the original values of vector

STEP 3: Call the function rev()  into variable Rev as Rev = rev(A)

STEP 4: print the vector Rev

R Source Code

                                          A= c(0, 10, 20, 30, 40, 50, 60)
print("Original vector is:")
print(A)
Rev = rev(A)
print("The vector in reverse order is:")
print(Rev)

                                      

OUTPUT

[1] "Original vector is:"
[1]  0, 10, 20, 30, 40, 50, 60
[1] "The vector in reverse order is:"
[1] 60 50 40 30 20 10  0