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:")
## [1] "Original vector is:"
print(A)
## [1]  0 10 20 30 40 50 60
Rev = rev(A)

print("The vector in reverse order is:")
## [1] "The vector in reverse order is:"
print(Rev)
## [1] 60 50 40 30 20 10  0

R Source Code

A = c(100, 110, 220, 330, 440, 550, 660)

print("Original vector is:")
## [1] "Original vector is:"
print(A)
## [1] 100 110 220 330 440 550 660
Rev = rev(A)

print("The vector in reverse order is:")
## [1] "The vector in reverse order is:"
print(Rev)
## [1] 660 550 440 330 220 110 100