1 Loop Through Vector in R

1.1 Example 1

for(i in 1:10) {
  
  x1 <- i^2
  print(x1)
}
## [1] 1
## [1] 4
## [1] 9
## [1] 16
## [1] 25
## [1] 36
## [1] 49
## [1] 64
## [1] 81
## [1] 100

1.2 Example 2

vec <- c(6, 3, 0, 9, 5)
vec
## [1] 6 3 0 9 5
length(vec)
## [1] 5
for(i in 1:length(vec)) {
  out <- vec[i] + 10
  print(out)
}
## [1] 16
## [1] 13
## [1] 10
## [1] 19
## [1] 15

1.3 Example 3:

a <- 1:10
b <- 1:10

res <- numeric(length = length(a))
for(i in seq_along(a)) {
  res[i] <- a[i] + b[i]
}

res
##  [1]  2  4  6  8 10 12 14 16 18 20

2 Loop over character vector

x2 <- c("Max", "Tina", "Lindsey", "Anton", "Sharon")
for(i in x2) {
  print(paste("The name", i, "consists of", nchar(i), "characters."))
}
## [1] "The name Max consists of 3 characters."
## [1] "The name Tina consists of 4 characters."
## [1] "The name Lindsey consists of 7 characters."
## [1] "The name Anton consists of 5 characters."
## [1] "The name Sharon consists of 6 characters."

3 Store for-Loop Results in Vector by Appending

x3 <- numeric()
for(i in 1:10) {
  
  x3 <- c(x3, i^2)
}
x3
##  [1]   1   4   9  16  25  36  49  64  81 100

4 Nested for-Loop in R

x4 <- character()
for(i in 1:5) {
  
  for(j in 1:3) {
    
    x4 <- c(x4, paste(LETTERS[i], letters[j], sep = "_"))
  }
}
x4
##  [1] "A_a" "A_b" "A_c" "B_a" "B_b" "B_c" "C_a" "C_b" "C_c" "D_a" "D_b" "D_c"
## [13] "E_a" "E_b" "E_c"

5 Break for-Loop Based on Logical Condition

for(i in 1:10) {
  
  x5 <- i^2
  print(x5)
  
  if(i >= 5) {
    
    break
  }
}
## [1] 1
## [1] 4
## [1] 9
## [1] 16
## [1] 25

Note: As you can see based on the previous output of the RStudio console, our for-loop was stopped when the index i was equal to 5

6 Continue to Next Iteration of for-Loop

for(i in 1:10) {
  
  if(i %in% c(1, 5, 7)) {
    
    next
  }
  
  x6 <- i^2
  print(x6)
}
## [1] 4
## [1] 9
## [1] 16
## [1] 36
## [1] 64
## [1] 81
## [1] 100

7 for-Loop Over Data Frame Columns

data("iris")
head(iris)
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa
iris_new2 <- iris
for(i in 1:ncol(iris_new2)) {
  
  colnames(iris_new2)[i] <- paste0("new_", i)
}
head(iris_new2)
##   new_1 new_2 new_3 new_4  new_5
## 1   5.1   3.5   1.4   0.2 setosa
## 2   4.9   3.0   1.4   0.2 setosa
## 3   4.7   3.2   1.3   0.2 setosa
## 4   4.6   3.1   1.5   0.2 setosa
## 5   5.0   3.6   1.4   0.2 setosa
## 6   5.4   3.9   1.7   0.4 setosa

8 for-Loop Through List Object

my_list <- list(1:5,
                letters[3:1],
                "XXX")
my_list
## [[1]]
## [1] 1 2 3 4 5
## 
## [[2]]
## [1] "c" "b" "a"
## 
## [[3]]
## [1] "XXX"
for(i in 1:length(my_list)) {
  
  my_list[[i]] <- rep(my_list[[i]], 3)
}
my_list
## [[1]]
##  [1] 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
## 
## [[2]]
## [1] "c" "b" "a" "c" "b" "a" "c" "b" "a"
## 
## [[3]]
## [1] "XXX" "XXX" "XXX"