R Programming: Reverse the order of given vector
R Programming: Vector Exercise-20 with Solution
Write a R program to reverse the order of given vector.
Sample Solution :
R Programming Code :
# Create a vector 'v' with a sequence of numbers
v = c(0, 10, 10, 10, 20, 30, 40, 40, 40, 50, 60)
# Print a message indicating the original vector
print("Original vector-1:")
# Print the original vector 'v'
print(v)
# Reverse the order of the vector 'v' and assign it to 'rv'
rv = rev(v)
# Print a message indicating the reversed vector
print("The said vector in reverse order:")
# Print the reversed vector 'rv'
print(rv)
Output:
[1] "Original vector-1:" [1] 0 10 10 10 20 30 40 40 40 50 60 [1] "The said vector in reverse order:" [1] 60 50 40 40 40 30 20 10 10 10 0
Explanation:
- Create a vector:
- v = c(0, 10, 10, 10, 20, 30, 40, 40, 40, 50, 60)
A vector v is created containing a sequence of numbers, including repeated values. - Print message (Original vector):
- print("Original vector-1:")
Prints the message "Original vector-1:" to indicate the following output will show the original vector. - Print the original vector:
- print(v)
Displays the content of the vector v (original order). - Reverse the vector:
- rv = rev(v)
Uses the rev() function to reverse the order of the elements in the vector v and stores the result in a new variable rv. - Print message (Reversed vector):
- print("The said vector in reverse order:")
Prints the message "The said vector in reverse order:" to indicate the next output will show the reversed vector. - Print the reversed vector:
- print(rv)
Displays the content of the reversed vector rv
R Programming Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a R program to find the elements of a given vector that are not in another given vector.
Next: Wrtie a R program to concatenate a vector.
Test your Programming skills with w3resource's quiz.
What is the difficulty level of this exercise?
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/r-programming-exercises/vector/r-programming-vector-exercise-20.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics