Open In App

Reversing a vector using a for loop?

Last Updated : 17 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss how to reverse a vector using a for loop with its working example in the R Programming Language using R for loop. Reversing the order of elements in a vector is a common operation in programming, often required for various tasks. One straightforward approach is to use a for loop to iterate through the vector and rearrange the elements in a reversed order. This method provides a fundamental way to achieve the desired outcome without relying on built-in functions. We will explore the steps involved, understand the underlying logic, and provide practical examples to illustrate the concept.

Syntax:

original_vector <- c(...)  # Replace ... with the vector elements
n <- length(original_vector)
for (i in 1:(n/2)) {
  temp <- original_vector[i]
  original_vector[i] <- original_vector[n - i + 1]
  original_vector[n - i + 1] <- temp
}

Example 1:

R




original_vector <- c(1, 2, 3, 4, 5)
 
n <- length(original_vector)
 
for (i in 1:(n/2)) {
  temp <- original_vector[i]
  original_vector[i] <- original_vector[n - i + 1]
  original_vector[n - i + 1] <- temp
}
 
print(original_vector)


Output:

[1] 5 4 3 2 1
  • Create a vector named original_vector with values 1, 2, 3, 4, 5.
  • Get the length of the original_vector and store it in the variable n.
  • Start a for loop that iterates from 1 to half of the vector’s length (2 iterations in this case).
  • Inside the loop:
  • Store the value of the element at index i of original_vector in a temporary variable called temp.
  • Replace the element at index i of original_vector with the element.
  • Replace the element.
  • Print the reversed original_vector using the print() function.

Example: Using a for loop with vector slicing to reverse a vector

R




# Original vector
original_vector <- c(1, 2, 3, 4, 5)
 
# Calculate the length of the vector
n <- length(original_vector)
 
# Initialize an empty vector to store the reversed elements
reversed_vector <- numeric(n)
 
# Using a for loop to reverse the vector using vector slicing
for (i in 1:n) {
  reversed_vector[i] <- original_vector[n - i + 1]
}
 
# Display the reversed vector
print(reversed_vector)


Output:

[1] 5 4 3 2 1
  • We start by defining an original vector containing elements [1, 2, 3, 4, 5].
  • We calculate the length of the original vector using the length() function and store it in the variable n.
  • We create an empty vector called reversed_vector to store the reversed elements. We initialize it with the numeric(n) function, ensuring it has the same length as the original vector.
  • We use a for loop with the loop variable i ranging from 1 to n. This loop iterates over each position in the original vector.
  • Inside the loop, we assign the value of the element at position n – i + 1 in the original vector to the corresponding position i in the reversed_vector. This effectively reverses the order of elements.

Example: Using a for loop with indexing

R




# Original vector
original_vector <- c(5, 10, 15, 20, 25)
 
# Calculate the length of the vector
n <- length(original_vector)
 
# Using a for loop with indexing to reverse the vector
for (i in 1:(n / 2)) {
  temp <- original_vector[i]
  original_vector[i] <- original_vector[n - i + 1]
  original_vector[n - i + 1] <- temp
}
 
# Display the reversed vector
print(original_vector)


Output:

[1] 25 20 15 10  5
  • We start by defining an original vector containing elements [5, 10, 15, 20, 25].
  • We calculate the length of the original vector using the length() function and store it in the variable n.
  • We use a for loop with the loop variable i ranging from 1 to (n / 2). This loop iterates over the first half of the original vector since we’re swapping elements using indexing.
  • Inside the loop, we use a temporary variable temp to hold the value of the element at position i in the original vector. Then, we update the element at position i with the value of the element at position n – i + 1, effectively swapping the elements. Finally, we update the element at position n – i + 1 with the value of temp.

Example 4:

R




original_vector <- c("apple", "banana", "cherry", "date")
 
n <- length(original_vector)
 
for (i in 1:(n/2)) {
  temp <- original_vector[i]
  original_vector[i] <- original_vector[n - i + 1]
  original_vector[n - i + 1] <- temp
}
 
print(original_vector)


Output:

[1] "date"   "cherry" "banana" "apple" 
  • Create a vector named original_vector .
  • Get the length of the original_vector and store it in the variable n.
  • Start a for loop that iterates from 1 to half of the vector’s length (2 iterations in this case).
  • Inside the loop:
  • Store the value of the element at index i of original_vector in a temporary variable called temp.
  • Replace the element at index i of original_vector with the element.
  • Replace the element.
  • Print the reversed original_vector using the print() function.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads