for-Loop with Increments in R (Example) | Increment by Size / Step 2

 

In this R programming tutorial you’ll learn how to write for-loops with increments by 2.

The post looks as follows:

It’s time to dive into the example:

 

Example: for-Loop with Larger Increments Using seq() Function

The following R code shows how to use larger increments in a for-loop. For this, we can use the seq function to specify the sequence over which we want to loop. Have a look at the following R code:

for(i in seq(from = 1, to = 10, by = 2)) {    # Running for-loop
  print(paste("This is iteration No.", i))
}
# [1] "This is iteration No. 1"
# [1] "This is iteration No. 3"
# [1] "This is iteration No. 5"
# [1] "This is iteration No. 7"
# [1] "This is iteration No. 9"

As you can see based on the previous output of the RStudio console, we have looped with increments by the size of 2, i.e. we skipped every second step.

 

Video & Further Resources

I have recently published a video on my YouTube channel, which explains the topics of this tutorial. You can find the video below.

 

The YouTube video will be added soon.

 

In addition, you may want to read the related tutorials of this homepage.

 

To summarize: In this R post you learned how to create for-loops with larger increments. Please tell me about it in the comments, in case you have any additional questions. Besides that, don’t forget to subscribe to my email newsletter to receive updates on the newest articles.

 

Subscribe to my free statistics newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


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.

Menu
Top