How to Write a while-Loop in R Programming (Example Code)

This tutorial shows how to write and use while-loops in the R programming language. while-loops are not as popular as for-loops, but can also be very useful.

Example: Basic Application of while-loop in R

my_data <- 5                    # Create example data to be used within while-loop
while(my_data <= 100) {         # Start running while-loop
  
  my_data <- my_data * 2        # Do some operation
  print(my_data)                # Print output of while-loop iteration
}
# [1] 10
# [1] 20
# [1] 40
# [1] 80
# [1] 160

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