How to Write a for-Loop with Numeric Range in R (0 Examples)

This post explains how to use for-loops with range in the R programming language.

example_range <- 3:7                # Creating example range
example_range                       # Printing example range
# [1] 3 4 5 6 7
my_output <- numeric()              # Creating empty numeric vector for output
for(i in example_range) {           # Starting for-loop
  my_output <- c(my_output, i^2)    # Some R code to create output
}
my_output                           # Printing output
# [1]  9 16 25 36 49

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

Menu
Top