Add New Element to Vector in for-Loop in R (Example Code)

In this R tutorial you’ll learn how to concatenate additional elements to a vector in a loop.

Example: Appending Additional Vector Elements in for-Loop

vector_object <- letters[1:3]            # Creating a vector object in R
vector_object                            # Show vector object in RStudio console
# "a" "b" "c"
for(index in 1:5) {                      # Starting the for-loop
  vector_object_new <- LETTERS[index]    # New value in each iteration
  vector_object <- c(vector_object,      # Add new value to original vector
vector_object                            # Show resulting vector in console
# "a" "b" "c" "A" "B" "C" "D" "E"

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed