R Nested Loops


Nested Loops

It is also possible to place a loop inside another loop. This is called a nested loop:

Example

Print the adjective of each fruit in a list:

adj <- list("red", "big", "tasty")

fruits <- list("apple", "banana", "cherry")
  for (x in adj) {
    for (y in fruits) {
      print(paste(x, y))
  }
}
Try it Yourself »

Copyright 1999-2023 by Refsnes Data. All Rights Reserved.