Open In App

How to Reverse the order of given vector in R ?

Improve
Improve
Like Article
Like
Save
Share
Report

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

  • create a vector
  • reverse the elements

Syntax to create a vector:

c(value1,value2,…,valuen)

Example: Creating the vector

R




# 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

R




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


Output:

reverse order of vector r



Last Updated : 30 May, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads