# range of numbers
for (i in 1:5) {
print(i)
}[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
| R | Example R | |
|---|---|---|
| for loop | for(){} | |
| While loop | while(){} |
Loops are a fundamental part of programming in data science because they allow you to efficiently perform repetitive tasks on large datasets. Instead of writing repetitive code manually, loops enable automation by iterating over data structures like lists, dataframes, or arrays. This is particularly important when processing datasets with thousands or millions of entries, as it saves time and reduces the risk of errors. Loops also allow for dynamic analysis, enabling you to apply functions, calculations, or transformations to each element in a dataset, making them a powerful tool for scalability and flexibility in data analysis.
Imagine you are analyzing the carbon footprint of 1,000 households to assess their environmental impact. Each household has data on electricity consumption, gas usage, and travel habits. Your task is to calculate the total carbon emissions for each household using a simple formula:
\[ CarbonEmissions=(Electricity(kWh) × 0.5) + (Gas(m³) × 2.1) + (Travel(km)×0.2) \]
Without loops, you would need to manually calculate this formula for each household, which is tedious and impractical. Instead, with a loop, you can automate the process.
Imagine that we have measurements of forestation over time:
| Year | Forest Area (ha) |
|---|---|
| 2000 | 1,000,000 |
| 2001 | 990,000 |
| 2002 | 975,000 |
| 2003 | 950,000 |
| 2004 | 930,000 |
| 2005 | 920,000 |
| 2005 | 900,000 |
If we want to compute the change in ha over time we would have to compute each year of change individually. Using a loop we automate this process by sequentially moving over the dataframe and computing the change rate. Let’s see this can be done in Python and R.
There are two main techniques for looping: a while loop and a for loop. The while loop needs a condition, while this condition is Ttrue, the loop will continue to run. The for loop is used when you want to repeat a task a specific number of times or iterate over a sequence (like a list, range, or string). It works by stepping through each element in the sequence until it reaches the end. Let’s have a look in detail:

A loop in R starts with the for operator. This is followed by an argument that determines how the loop runs (“the loop runs for…”). We start by defining a variable that will take the different values in the loop. Suppose we want to print the value 1 to 5. This requires a loop that takes a variables that starts at 1, and increases by one with each iteration. The in operator is used to define the values the variables will take.
In the following code we will show different ways to loop:
In this first example the code will start with i = 1, then it runs that code between brackets, so it will print out “1”. It then automatically moves to 2, prints, moves to 3 etc. until i = 5. You can put any number of operations between the curly brackets:
[1] 1
[1] 1
[1] 2
[1] 2
[1] 3
[1] 3
[1] 4
[1] 4
[1] 5
[1] 5
By default the for operator will use a step of 1 (1, 2, 3, 4, 5). We can use different steps by setting th second argument as a vector that only contains the elements that we want i to take. seq(1, 10, by = 2) creates a vector starting at 1 stopping at 10 with a step of 2, this means that it creates a vector with: 1,3,5,7,9. i then iterates over this list taking only those values:
[1] "Odd number: 1"
[1] "Odd number: 3"
[1] "Odd number: 5"
[1] "Odd number: 7"
[1] "Odd number: 9"
The elements we use for the loop do not have to be numbers, they can also take the form of text, instead of using a vector with numbers, we use a vector with words for example:
# items in a vector
fruits <- c("apple", "banana", "cherry", "date")
for (fruit in fruits) {
print(fruit)
}[1] "apple"
[1] "banana"
[1] "cherry"
[1] "date"
# Loop with an index
languages <- c("Python", "Java", "C++", "Ruby")
for (i in 1:length(languages)) {
cat("Language", i, "is", languages[i], "\n")
}Language 1 is Python
Language 2 is Java
Language 3 is C++
Language 4 is Ruby
Of course it is possible to complexify loops by nesting them.
i = 1 , j = 1
i = 1 , j = 2
i = 2 , j = 1
i = 2 , j = 2
i = 3 , j = 1
i = 3 , j = 2
While loops continue to loop as long as a specific condition is satisfied. They therefore differ from the for loops which have a specified stopping point. The danger with these loops is that they can theoretically run forever if the conditions is always verified. The basic logic of these loops is: while followed by a condition and then the code to execute while this condition is verified:

# Example 1: Simple while loop
count <- 1
while (count <= 5) {
cat("Iteration", count, "\n")
count <- count + 1}Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5
The main point of the while loops is that the execution of the code should update something that bring the condition closer to being verified. In the example above, we increase the count variable each time we run the loop until it’s equal to a certain value. If you use a while loop but nothing in the script is updated, the loop will run forever.
# Example 2: Loop with a condition and next statement (equivalent to continue in Python)
i <- 0
while (i < 10) {
i <- i + 1
if (i %% 2 == 0) {
next } # Skip even numbers
cat(i, "\n")}1
3
5
7
9
# Example 3: Nested while loops
row <- 1
while (row <= 3) {
col <- 1
while (col <= 3) {
cat("Row", row, "Column", col, "\n")
col <- col + 1 }
row <- row + 1}Row 1 Column 1
Row 1 Column 2
Row 1 Column 3
Row 2 Column 1
Row 2 Column 2
Row 2 Column 3
Row 3 Column 1
Row 3 Column 2
Row 3 Column 3

# Example 1: Simple while loop
count = 1
while count <= 5:
print("Iteration", count)
count += 1
# Example 3: Loop with a condition and continue statement
i = 0
while i < 10:
i += 1
if i % 2 == 0:
continue # Skip even numbers
print(i)
# Example 4: Nested while loops
row = 1
while row <= 3:
col = 1
while col <= 3:
print("Row", row, "Column", col)
col += 1
row += 1Use a for loop to iterate over a range of numbers and a while loop to calculate cumulative sums.
On blackboard you wil find a dataset with energy production per county. Your task is to analyze this data to gain insights into the energy trends of these regions.
In the exam there will be different types of questions.
You are provided with an R script. Explain each line of code of the script.
discounted_prices <- c()
for (i in 1:nrow(products)) {
price <- products$Price[i]
in_stock <- products$InStock[i]
if (price > 50 && in_stock) {
new_price <- price * 0.9
} else if (price <= 50 && in_stock) {
new_price <- price * 0.95
} else {
new_price <- price
}
discounted_prices <- c(discounted_prices, new_price)
}
products$DiscountedPrice <- discounted_prices
print(products)You are in charge of a log-in system on a website in which a user has to provide the correct password. When a correct password is provides print “success!”. If the user has failed 3 times stop the system. Write a function that can perform this task.