Loops | Dataplexa

Loops in R

In this lesson, you will learn how loops work in R. Loops allow a program to repeat a set of instructions multiple times automatically.

Loops are essential when you need to process multiple values, work with datasets, or perform repetitive tasks efficiently.


What Is a Loop?

A loop is a programming structure that executes the same block of code repeatedly until a specific condition is met.

Instead of writing the same code again and again, loops help reduce effort, avoid errors, and make programs cleaner.


The for Loop

The for loop is the most commonly used loop in R. It runs once for each value in a sequence.

This loop is ideal when you know in advance how many times the code should run.

for (i in 1:5) {
  print(i)
}

Here, the loop prints numbers from 1 to 5, one by one.


Looping Over a Vector

You can use a for loop to iterate over elements of a vector.

This is useful when performing operations on each value individually.

numbers <- c(10, 20, 30)

for (value in numbers) {
  print(value * 2)
}

Each element in the vector is processed once during the loop.


Using seq() in Loops

The seq() function helps control how the loop progresses.

It is useful when you want to skip values or use custom step sizes.

for (i in seq(1, 10, by = 2)) {
  print(i)
}

This loop prints only odd numbers between 1 and 10.


The while Loop

The while loop continues running as long as a condition remains true.

It is useful when the number of repetitions is not known beforehand.

count <- 1

while (count <= 5) {
  print(count)
  count <- count + 1
}

Always ensure the condition eventually becomes false to avoid infinite loops.


The repeat Loop

The repeat loop runs indefinitely until explicitly stopped.

It must include a break statement to exit the loop.

x <- 1

repeat {
  print(x)
  x <- x + 1

  if (x > 3) {
    break
  }
}

This loop stops once the condition inside break is met.


The break Statement

The break statement immediately stops a loop.

It is often used when a specific condition is reached.

for (i in 1:10) {
  if (i == 5) {
    break
  }
  print(i)
}

The loop exits when the value reaches 5.


The next Statement

The next statement skips the current iteration and moves to the next one.

This is useful when certain values should be ignored.

for (i in 1:5) {
  if (i == 3) {
    next
  }
  print(i)
}

The number 3 is skipped during execution.


Loops with Data Frames

Loops can process rows or columns of a data frame.

This is helpful when applying custom logic to each record.

df <- data.frame(values = c(2, 4, 6))

for (i in df$values) {
  print(i * 3)
}

Each value is multiplied and printed one by one.


Why Loops Matter in R

Loops help automate repetitive tasks and process data efficiently.

They are widely used in data cleaning, calculations, simulations, and reporting.


📝 Practice Exercises


Exercise 1

Write a for loop to print numbers from 1 to 10.

Exercise 2

Use a while loop to print numbers until the value reaches 5.

Exercise 3

Create a loop that skips even numbers using next.

Exercise 4

Use a loop to calculate the square of each value in a vector.


✅ Practice Answers


Answer 1

for (i in 1:10) {
  print(i)
}

Answer 2

x <- 1

while (x <= 5) {
  print(x)
  x <- x + 1
}

Answer 3

for (i in 1:10) {
  if (i %% 2 == 0) {
    next
  }
  print(i)
}

Answer 4

values <- c(2, 4, 6)

for (v in values) {
  print(v ^ 2)
}

What’s Next?

In the next lesson, you will learn about basic functions in R.

Functions help organize code, reduce repetition, and improve readability.