Open In App

How to Reverse the order of given vector in R ?

In this article, we will discuss how to reverse the order of elements in a vector using rev() function in R Programming Language.

Syntax:



rev(vector_name)

Approach



Syntax to create a vector:

c(value1,value2,…,valuen)

Example: Creating the vector




# create vector with names
name=c("sravan","mohan","sudheer","radha","vani","mohan")
  
# create vector with subjects
subjects=c(".net","Python","java","dbms","os","dbms")
  
# create a vector with marks
marks=c(98,97,89,90,87,90)
  
# create vector with height
height=c(5.97,6.11,5.89,5.45,5.78,6.0)
  
# create vector with weight
weight=c(67,65,78,65,81,76)
  
# pass these vectors to the vector
data=c(name,subjects,marks,height,weight)
  
# display
print(data)

Output:

Example: Reverse the order of elements




print("reverse")
print(rev(name))
print(rev(subjects))
print(rev(marks))
print(rev(height))
print(rev(weight))

Output:


Article Tags :