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
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.
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
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)
[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