R Basics

4. For Loops

Everything that we have seen so far has involved us giving a program a single instruction and asking it to run. This is not particularly efficient if we want to do the same thing several times over. We would much rather find a way to automate this and run the task multiple times for us, for loops are the perfect way to get started here.

Lists


We cover lists and other group concepts elsewhere, but a list is simply a way to group lots of things into one variable. A list has lots of functions that make it easy to work with – please see its own page for more on this. This list is a 3-a-side team selected to play in a TV commercial.

             
    TeamA = c("Nasta","Overvenus","Ranalda")
             
     

The TV commercial wants to announce these players to the crowd. We can do this individually for each:

             
   print(paste0("Welcome to the cage, ", TeamA[1]))

   ## [1] "Welcome to the cage, Nasta"
                          
     
             
   print(paste0("Welcome to the cage, ", TeamA[2]))

   ## [1] "Welcome to the cage, Overvenus"
                          
     
             
   print(paste0("Welcome to the cage, ", TeamA[3]))

   ## [1] "Welcome to the cage, Ranalda"
                          
     

Notice how we repeated ourself three times? 'For loops' help us to avoid doing so. The smarter announcer is run using the following code:

             
   for (player in TeamA) {
     print(paste0("Welcome to the cage, ", player))
   }
             
     

Our output is exactly the same, but the code looks so much better!

The essential bits to our code are the words ‘for’ and ‘in’, ***

Plus, we could also add a second team really really easily with a ‘nested for-loop’ – this is a for-loop inside a for-loop!

             
   #Team 2, enter!
   TeamB = c("Tatti","Nikita","Hanry")

   #A list of lists!
   Teams <- c(TeamA,TeamB)

   for (team in Teams) {
     for (player in team) {
       print(paste0("Welcome to the cage, ", player))
     }
   }

    ## [1] "Welcome to the cage, Nasta"
    ## [1] "Welcome to the cage, Overvenus"
    ## [1] "Welcome to the cage, Ranalda"
    ## [1] "Welcome to the cage, Tatti"
    ## [1] "Welcome to the cage, Nikita"
    ## [1] "Welcome to the cage, Hanry"
             
     

             
    TeamA = c("Nasta","Overvenus","Ranalda")
             
     

So cool!

Summary


If we have to write down every single thing that we want our code to do, it will be hugely inefficient and a bit of a pain to write. We can utilise for loops to automate any repetition and to try our best to avoid duplicate code. In fact, programming has a DRY mantra – “Don’t repeat yourself”.

Our examples above demonstrate a nested for-loop too – a loop within a loop to push our automation even further.

Give them a try yourself, and check out while loops when you’re comfortable here!

Next Lessons...


  1. Numbers, Variables & Strings
  2. Comparisons & Logic
  3. If Statements
  4. For Loops
  5. While Loops
  6. Lists
  7. Packages
  8. Creating Functions
  9. Random Numbers with Expected Goals

This is a R conversion of a tutorial by FC Python. I take no credit for the idea and have their blessing to make this conversion. All text is a direct copy unless changes were relevant. Please follow them on twitter and if you have a desire to learn Python then they are a fantastic resource!