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 iteration deals with the input No.", i)) } # [1] "This iteration deals with the input No. 1" # [1] "This iteration deals with the input No. 3" # [1] "This iteration deals with the input No. 5" # [1] "This iteration deals with the input No. 7" # [1] "This iteration deals with the input No. 9" |
for(i in seq(from = 1, to = 10, by = 2)) { # Running for-loop print(paste("This iteration deals with the input No.", i)) } # [1] "This iteration deals with the input No. 1" # [1] "This iteration deals with the input No. 3" # [1] "This iteration deals with the input No. 5" # [1] "This iteration deals with the input No. 7" # [1] "This iteration deals with the input 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.
Is the Phrase “Loop with Increments” Correct?
The code of Example 1 shows a simple example on how to loop over a sequence containing every second integer. However, you may also define your own numbers for the loop as shown below:
for(i in c(3, 7, 42, 1)) { print(paste("For this run of the loop, we are using the number", i)) } # [1] "For this run of the loop, we are using the number 3" # [1] "For this run of the loop, we are using the number 7" # [1] "For this run of the loop, we are using the number 42" # [1] "For this run of the loop, we are using the number 1" |
for(i in c(3, 7, 42, 1)) { print(paste("For this run of the loop, we are using the number", i)) } # [1] "For this run of the loop, we are using the number 3" # [1] "For this run of the loop, we are using the number 7" # [1] "For this run of the loop, we are using the number 42" # [1] "For this run of the loop, we are using the number 1"
You may even loop through a vector of character strings:
for(i in c("bananas", "pineapples", "strawberries")) { print(paste("I like the juice of", i)) } # [1] "I like the juice of bananas" # [1] "I like the juice of pineapples" # [1] "I like the juice of strawberries" |
for(i in c("bananas", "pineapples", "strawberries")) { print(paste("I like the juice of", i)) } # [1] "I like the juice of bananas" # [1] "I like the juice of pineapples" # [1] "I like the juice of strawberries"
So technically speaking it would be more precise to say something like “we are looping over every second integer” instead of “we are looping with increments”.
Some parts of the code above are based on a comment of Nils Petras in the Statistics Globe Facebook group. Thanks a lot to him for his contribution!
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.
2 Comments. Leave new
Hi Joachim. Thank you for sharing this great tutorial on for loop. I hope you can have a tutorial on nested for loop since i always find it hard to understand. Thank you.
Hey Hao,
Thank you, glad you find the tutorial helpful!
Please have a look at this tutorial on nested loops.
Regards,
Joachim