R Program to reverse the order of given vector

How to reverse the order of a given vector

Here we are explaining how to reverse the order of a given vector. Here we are using a built-in function rev() for this purpose. The rev() function helps to give a reversed version of its argument. The syntax of this function is like

rev(x) #Where x is a vector or object which needs to be reversed
 

How to reverse the order of a given vector in R program

Below are the steps used in the R program to get the reverse of a given vector. In this R program, we directly give the values to built-in functions. And print the function result. Here we used variable A for assigning vector values. And Rev for assigning reversed vector.

ALGORITHM

STEP 1: Assign variable A with vector values

STEP 2: First print original vector values

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