seq(from = 0, to = 10, by = 2) # 0, 2, 4, 6, 8, 10[1] 0 2 4 6 8 10
seq(0, 1, by = 0.1) # 0.0, 0.1, ..., 1.0 [1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
seq_along(vector) # Indices for a vector[1] 1
Steven P. Sanderson II, MPH
November 17, 2025
Programming, for loop in R, R for loop range, R programming for loop, R loop examples, how to use for loop in R, R loop syntax, iterate over range in R, R for loop best practices, R code automation, R data analysis loops, how to write a for loop with range in R, best way to loop through numbers in R programming, practical examples of for loops in R for beginners, step-by-step guide to using for loops in R, efficient for loop techniques for data analysis in R
Key Takeaway:
Mastering for-loops with ranges in R empowers you to process data, automate repetitive tasks, and write clear, efficient code. This guide covers everything from basic syntax to advanced techniques, with plenty of working examples and best practices.
For-loops are a cornerstone of R programming, allowing you to repeat actions for each element in a sequence or range. Whether you’re iterating over numbers, vectors, or data frames, understanding how to use ranges in for-loops is essential for data analysis, automation, and algorithm development.
This article will walk you through the essentials of for-loops with ranges in R, from simple counting to advanced applications, with clear, working code examples and practical tips.
A for-loop in R is a control structure that repeats a block of code for each value in a sequence (range). It’s especially useful when you need to perform repetitive tasks, process data row by row, or apply custom logic that isn’t easily vectorized .
The general syntax is:
variable takes each value from sequence in turn.{} runs for each value.:)The simplest way to create a range in R is with the colon operator:
For more control (custom steps, decimals), use seq():
[1] "Even number: 0"
[1] "Even number: 2"
[1] "Even number: 4"
[1] "Even number: 6"
[1] "Even number: 8"
[1] "Even number: 10"
[1] "Even number: 12"
[1] "Even number: 14"
[1] "Even number: 16"
[1] "Even number: 18"
[1] "Even number: 20"
# Sine values for decimals between 0 and 1
for (x in seq(0, 1, by = 0.1)) {
print(paste("sin(", round(x, 1), ") =", round(sin(x), 3)))
}[1] "sin( 0 ) = 0"
[1] "sin( 0.1 ) = 0.1"
[1] "sin( 0.2 ) = 0.199"
[1] "sin( 0.3 ) = 0.296"
[1] "sin( 0.4 ) = 0.389"
[1] "sin( 0.5 ) = 0.479"
[1] "sin( 0.6 ) = 0.565"
[1] "sin( 0.7 ) = 0.644"
[1] "sin( 0.8 ) = 0.717"
[1] "sin( 0.9 ) = 0.783"
[1] "sin( 1 ) = 0.841"
| Practice | Do This | Avoid This |
|---|---|---|
Use seq_along() |
for (i in seq_along(vec)) |
for (i in 1:length(vec)) (fails if empty) |
| Pre-allocate vectors | numeric(n) |
Growing vectors in loop |
| Check range direction | 1:10 or 10:1 |
1:0 (creates c(1,0)) |
| Use vectorization | When possible, for speed | Overusing for-loops |
Write a for-loop that calculates the running average of a numeric vector.
running_avg <- numeric(length(scores))
for (i in 1:length(scores)) {
running_avg[i] <- mean(scores[1:i])
print(paste("Position", i, "- Running Average:", round(running_avg[i], 2)))
}[1] "Position 1 - Running Average: 85"
[1] "Position 2 - Running Average: 88.5"
[1] "Position 3 - Running Average: 85"
[1] "Position 4 - Running Average: 87.75"
[1] "Position 5 - Running Average: 88"
[1] "Position 6 - Running Average: 89"
[1] "Position 7 - Running Average: 88.71"
[1] "Position 8 - Running Average: 89"
: for simple integer ranges, seq() for custom steps or decimals.seq_along() over 1:length() for safe iteration.break and next for flexible loop control.Q1: When should I use : vs seq()?
A1: Use : for simple, consecutive integers. Use seq() for custom steps, decimals, or non-integer sequences.
Q2: Can I change the loop variable inside the loop?
A2: You can, but it won’t affect the sequence of values the loop iterates over.
Q3: What’s the difference between for-loops and apply functions?
A3: For-loops offer explicit control and are easier to debug; apply functions are concise and often faster for simple operations.
Q4: How do I avoid errors with empty vectors?
A4: Use seq_along() or seq_len() instead of 1:length() to handle empty vectors safely.
Q5: What happens if I use 1:0 in a for-loop?
A5: It creates the sequence c(1, 0), so the loop runs twice. Use seq_len() for zero-length safety.
For-loops with ranges are a powerful tool in R, enabling you to automate repetitive tasks, process data, and implement custom logic. By mastering range creation, safe iteration, and best practices, you’ll write more robust and efficient R code. Practice with the examples above and integrate these techniques into your projects for better results.
Enjoyed this guide?
Share it with your fellow R programmers and bookmark it for future reference!
Happy Coding! 🚀

You can connect with me at any one of the below:
Telegram Channel here: https://t.me/steveondata
LinkedIn Network here: https://www.linkedin.com/in/spsanderson/
Mastadon Social here: https://mstdn.social/@stevensanderson
RStats Network here: https://rstats.me/@spsanderson
GitHub Network here: https://github.com/spsanderson
Bluesky Network here: https://bsky.app/profile/spsanderson.com
My Book: Extending Excel with Python and R here: https://packt.link/oTyZJ
You.com Referral Link: https://you.com/join/EHSLDTL6
@online{
for_loop_with_range_in_r_a_complete_guide_with_practical_examples_20251117,
author = {Sanderson II MPH, Steven P.},
title = {For-Loop with Range in R: A Complete Guide with Practical Examples},
date = {2025-11-17},
url = {https://www.spsanderson.com/steveondata/posts/2025-11-17/},
langid = {en}
}