Open In App

How to Reverse the order of given vector in R ?

Last Updated : 30 May, 2021
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



Similar Reads

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 in R Programming Language. It can be done using the rev() function. It returns the reverse version of data objects. Syntax: rev(x) Parameter: x: Data object Returns: Reverse of the data object passed Example 1: Here we will create a vector and reverse it with rev() fu
2 min read
Order DataFrame rows according to vector with specific order in R
In this article, we will see how to sort data frame rows based on the values of a vector with a specific order. There are two functions by which we can sort data frame rows based on the values of a vector. match() functionleft_join() function Example dataset: data <- data.frame(x1 = 1:5, x2 = letters[1:5], x3 = 6:10) data x1 x2 x3 1 1 a 6 2 2 b
3 min read
How to create a new vector from a given vector in R
In this article, we will discuss How to create a new vector from a given vector in R Programming Language. Create a new vector from a given vectorYou can use various functions and techniques depending on your needs to create a new vector from a given vector in R. Here are some common methods. 1. Subset the VectorIf you want to create a new vector w
2 min read
Convert an Object into a Vector in R Programming - as.vector() Function
as.vector() function in R Language is used to convert an object into a vector. Syntax: as.vector(x) Parameters: x: Object to be converted Example 1: # R program to convert an object to vector # Creating an array x <- array(c(2, 3, 4, 7, 2, 5), c(3, 2)) x # Calling as.vector() Function as.vector(x) Output: [, 1] [, 2] [1, ] 2 7 [2, ] 3 2 [3, ] 4
1 min read
Check for the Existence of a Vector Object in R Programming - is.vector() Function
is.vector() function in R Language is used to return TRUE if the given vector is of the specified mode having no attributes other than names. It returns FALSE otherwise. Syntax: is.vector(x, mode = "any") Parameters: x: R object mode: character string naming an atomic mode or "list" or "expression" or "any" Example 1: # R program to illustrate # is
1 min read
Create a Vector of Colors from a Vector of Gray Levels in R Programming - gray() or grey() Function
gray() or grey() function in R Language is used to create a vector of colors from a vector of gray levels. Syntax: gray(level) or grey(level) Parameters: level: a vector of desired gray levels between 0 and 1; zero indicates "black" and one indicates "white". Example 1: # R program to illustrate # gray function # Calling the gray() function gray(0)
1 min read
Find the elements of a vector that are not in another vector in R
Two vectors can hold some values common. This article discusses how can we find set difference of these vectors i.e. display elements which are present in one vector but not in the other. If we want all the elements of a vector that are not in another vector then we can use setdiff() method in R. It takes two vectors and returns a new vector with t
1 min read
How to reverse the order of a dataframe in R?
Order reversal of the dataframe involves swapping of the rows or columns in such a way, that the elements are accessed from backward. In this article, we will discuss various ways of reversing the order of a dataframe both column and row-wise. Reversing the order of columns Method 1: Using the rev method The rev() method in R is used to return the
5 min read
How to check if a vector contains given value in R ?
We can check if a vector contains a given value using the %in% operator. For this, we have to create a vector with some values. And we have to read input from the user for what value to be checked. Or we can assign some value to a variable explicitly. Using the %in% operator with the below-given syntax we can find the element we are looking for. Sy
1 min read
How to test if a vector contains the given element in R ?
In this article, let's discuss how to check a specific element in a vector in R Programming Language. Method 1: Using loop A for loop can be used to check if the element belongs to the vector. A boolean flag can be declared and initialized to False. As soon as the element is contained in the vector, the flag value is set to TRUE. The element is pre
3 min read