Loops and conditions

1

Note

This chapter is optional.

# install.packages("tidyverse")
# install.packages("here")

library(tidyverse)
library(here)

athletes <- readRDS(file = here::here("raw_data", "athletes.rds"))
medal_counts <- athletes %>%
  filter(Medal == "Gold") %>%
  group_by(Region) %>%
  count(Medal) 

For-loops

Motivation

When programming, you often want to repeat an operation multiple times. For example, assume you want to print the number of gold medals for each country into your console. The most intuitive way would be to just copy paste the code with changed country names:

print(
  paste0(
    medal_counts[medal_counts$Region == "Algeria", "n"], 
    " Olympic gold medalists are from ", 
    medal_counts[medal_counts$Region == "Algeria", "Region"], 
    ".")
  )
[1] "5 Olympic gold medalists are from Algeria."
print(
  paste0(
    medal_counts[medal_counts$Region == "Argentina", "n"], 
    " Olympic gold medalists are from ", 
    medal_counts[medal_counts$Region == "Argentina", "Region"], 
    ".")
  )
[1] "91 Olympic gold medalists are from Argentina."
print(
  paste0(
    medal_counts[medal_counts$Region == "Armenia", "n"], 
    " Olympic gold medalists are from ", 
    medal_counts[medal_counts$Region == "Armenia", "Region"], 
    ".")
  )
[1] "2 Olympic gold medalists are from Armenia."
print(
  paste0(medal_counts[medal_counts$Region == "Australia", "n"], 
         " Olympic gold medalists are from ", 
         medal_counts[medal_counts$Region == "Australia", "Region"],
         ".")
  )
[1] "368 Olympic gold medalists are from Australia."

This could go on …
Here we paste together a sentence consisting of data and some pre specified character strings, which we then print into the console. However, this can become pretty tedious if we want to add more countries, and what if we want to change something in the print statement? In this case, we would have to go over all rows and change it multiple times. So, let’s write a loop that does that automatically for us:

for(i in unique(medal_counts$Region)){
  print(
    paste0(
      medal_counts[medal_counts$Region == i, "n"], 
      " Olympic gold medalists are from ", 
      i, 
      "." )
    )
}
[1] "5 Olympic gold medalists are from Algeria."
[1] "91 Olympic gold medalists are from Argentina."
[1] "2 Olympic gold medalists are from Armenia."
[1] "368 Olympic gold medalists are from Australia."
[1] "108 Olympic gold medalists are from Austria."
[1] "7 Olympic gold medalists are from Azerbaijan."
[1] "14 Olympic gold medalists are from Bahamas."
[1] "1 Olympic gold medalists are from Bahrain."
[1] "24 Olympic gold medalists are from Belarus."
[1] "98 Olympic gold medalists are from Belgium."
[1] "109 Olympic gold medalists are from Brazil."
[1] "54 Olympic gold medalists are from Bulgaria."
[1] "1 Olympic gold medalists are from Burundi."
[1] "20 Olympic gold medalists are from Cameroon."
[1] "463 Olympic gold medalists are from Canada."
[1] "3 Olympic gold medalists are from Chile."
[1] "351 Olympic gold medalists are from China."
[1] "5 Olympic gold medalists are from Colombia."
[1] "1 Olympic gold medalists are from Costa Rica."
[1] "58 Olympic gold medalists are from Croatia."
[1] "164 Olympic gold medalists are from Cuba."
[1] "123 Olympic gold medalists are from Czech Republic."
[1] "179 Olympic gold medalists are from Denmark."
[1] "3 Olympic gold medalists are from Dominican Republic."
[1] "1 Olympic gold medalists are from Ecuador."
[1] "7 Olympic gold medalists are from Egypt."
[1] "13 Olympic gold medalists are from Estonia."
[1] "22 Olympic gold medalists are from Ethiopia."
[1] "13 Olympic gold medalists are from Fiji."
[1] "198 Olympic gold medalists are from Finland."
[1] "501 Olympic gold medalists are from France."
[1] "8 Olympic gold medalists are from Georgia."
[1] "1301 Olympic gold medalists are from Germany."
[1] "62 Olympic gold medalists are from Greece."
[1] "1 Olympic gold medalists are from Grenada."
[1] "1 Olympic gold medalists are from Haiti."
[1] "432 Olympic gold medalists are from Hungary."
[1] "138 Olympic gold medalists are from India."
[1] "1 Olympic gold medalists are from Individual Olympic Athletes."
[1] "11 Olympic gold medalists are from Indonesia."
[1] "18 Olympic gold medalists are from Iran."
[1] "9 Olympic gold medalists are from Ireland."
[1] "1 Olympic gold medalists are from Israel."
[1] "575 Olympic gold medalists are from Italy."
[1] "1 Olympic gold medalists are from Ivory Coast."
[1] "38 Olympic gold medalists are from Jamaica."
[1] "247 Olympic gold medalists are from Japan."
[1] "1 Olympic gold medalists are from Jordan."
[1] "20 Olympic gold medalists are from Kazakhstan."
[1] "34 Olympic gold medalists are from Kenya."
[1] "1 Olympic gold medalists are from Kosovo."
[1] "3 Olympic gold medalists are from Latvia."
[1] "2 Olympic gold medalists are from Liechtenstein."
[1] "6 Olympic gold medalists are from Lithuania."
[1] "4 Olympic gold medalists are from Luxembourg."
[1] "30 Olympic gold medalists are from Mexico."
[1] "2 Olympic gold medalists are from Mongolia."
[1] "6 Olympic gold medalists are from Morocco."
[1] "1 Olympic gold medalists are from Mozambique."
[1] "1 Olympic gold medalists are from Nepal."
[1] "287 Olympic gold medalists are from Netherlands."
[1] "90 Olympic gold medalists are from New Zealand."
[1] "23 Olympic gold medalists are from Nigeria."
[1] "16 Olympic gold medalists are from North Korea."
[1] "378 Olympic gold medalists are from Norway."
[1] "42 Olympic gold medalists are from Pakistan."
[1] "1 Olympic gold medalists are from Panama."
[1] "1 Olympic gold medalists are from Peru."
[1] "117 Olympic gold medalists are from Poland."
[1] "4 Olympic gold medalists are from Portugal."
[1] "1 Olympic gold medalists are from Puerto Rico."
[1] "161 Olympic gold medalists are from Romania."
[1] "1599 Olympic gold medalists are from Russia."
[1] "157 Olympic gold medalists are from Serbia."
[1] "15 Olympic gold medalists are from Slovakia."
[1] "8 Olympic gold medalists are from Slovenia."
[1] "32 Olympic gold medalists are from South Africa."
[1] "221 Olympic gold medalists are from South Korea."
[1] "110 Olympic gold medalists are from Spain."
[1] "1 Olympic gold medalists are from Suriname."
[1] "479 Olympic gold medalists are from Sweden."
[1] "175 Olympic gold medalists are from Switzerland."
[1] "1 Olympic gold medalists are from Syria."
[1] "3 Olympic gold medalists are from Taiwan."
[1] "1 Olympic gold medalists are from Tajikistan."
[1] "9 Olympic gold medalists are from Thailand."
[1] "7 Olympic gold medalists are from Trinidad."
[1] "3 Olympic gold medalists are from Tunisia."
[1] "40 Olympic gold medalists are from Turkey."
[1] "678 Olympic gold medalists are from UK."
[1] "2638 Olympic gold medalists are from USA."
[1] "2 Olympic gold medalists are from Uganda."
[1] "47 Olympic gold medalists are from Ukraine."
[1] "1 Olympic gold medalists are from United Arab Emirates."
[1] "31 Olympic gold medalists are from Uruguay."
[1] "10 Olympic gold medalists are from Uzbekistan."
[1] "2 Olympic gold medalists are from Venezuela."
[1] "1 Olympic gold medalists are from Vietnam."
[1] "17 Olympic gold medalists are from Zimbabwe."

So this little piece of code can do what we started to do in the above code section, but already for all countries. Also, if we want to change something, we only have to change it once.

Tip

In general, try not to repeat yourself when writing code. As a general rule of thumb, use loops and/or functions if you need to copy/paste something more than two times. In the above example, we could also have put the paste() call into a function of it own, to trim down the code even more and make it more readable.

How to write a for-loop?

Let’s take a step back and look at what we are actually doing here. A for loop is generally constructed like this:

for(counter in values){
  repeat something with changing counter
}

What does this mean? Let’s look at a simple example first. We want to multiply the values from 10 to 20 with 100 and then print them into our console:

for(i in 10:20){
  new_i <- i * 100
  print(new_i)
}
[1] 1000
[1] 1100
[1] 1200
[1] 1300
[1] 1400
[1] 1500
[1] 1600
[1] 1700
[1] 1800
[1] 1900
[1] 2000
  1. In the first iteration, i takes the value of the first vector element behind in, which is 10.
  2. The operation in the loop body gets executed, in this case the current value gets multiplied by 100 and then printed into the console.
  3. After the operation is finished, i takes the next value, in this case 11, and the whole process starts again.
  4. The loop is finished after the operation on the last value has been executed, in this case 20.

Let’s get back to our initial example. Try to figure out what happens here by yourself. What is the first and the last value i gets assigned?

for(i in unique(medal_counts$Region)){
  print(
    paste0(
      medal_counts[medal_counts$Region == i, "n"], 
      " Olympic gold medalists are from ", 
      i, 
      "." )
    )
}
[1] "5 Olympic gold medalists are from Algeria."
[1] "91 Olympic gold medalists are from Argentina."
[1] "2 Olympic gold medalists are from Armenia."
[1] "368 Olympic gold medalists are from Australia."
[1] "108 Olympic gold medalists are from Austria."
[1] "7 Olympic gold medalists are from Azerbaijan."
[1] "14 Olympic gold medalists are from Bahamas."
[1] "1 Olympic gold medalists are from Bahrain."
[1] "24 Olympic gold medalists are from Belarus."
[1] "98 Olympic gold medalists are from Belgium."
[1] "109 Olympic gold medalists are from Brazil."
[1] "54 Olympic gold medalists are from Bulgaria."
[1] "1 Olympic gold medalists are from Burundi."
[1] "20 Olympic gold medalists are from Cameroon."
[1] "463 Olympic gold medalists are from Canada."
[1] "3 Olympic gold medalists are from Chile."
[1] "351 Olympic gold medalists are from China."
[1] "5 Olympic gold medalists are from Colombia."
[1] "1 Olympic gold medalists are from Costa Rica."
[1] "58 Olympic gold medalists are from Croatia."
[1] "164 Olympic gold medalists are from Cuba."
[1] "123 Olympic gold medalists are from Czech Republic."
[1] "179 Olympic gold medalists are from Denmark."
[1] "3 Olympic gold medalists are from Dominican Republic."
[1] "1 Olympic gold medalists are from Ecuador."
[1] "7 Olympic gold medalists are from Egypt."
[1] "13 Olympic gold medalists are from Estonia."
[1] "22 Olympic gold medalists are from Ethiopia."
[1] "13 Olympic gold medalists are from Fiji."
[1] "198 Olympic gold medalists are from Finland."
[1] "501 Olympic gold medalists are from France."
[1] "8 Olympic gold medalists are from Georgia."
[1] "1301 Olympic gold medalists are from Germany."
[1] "62 Olympic gold medalists are from Greece."
[1] "1 Olympic gold medalists are from Grenada."
[1] "1 Olympic gold medalists are from Haiti."
[1] "432 Olympic gold medalists are from Hungary."
[1] "138 Olympic gold medalists are from India."
[1] "1 Olympic gold medalists are from Individual Olympic Athletes."
[1] "11 Olympic gold medalists are from Indonesia."
[1] "18 Olympic gold medalists are from Iran."
[1] "9 Olympic gold medalists are from Ireland."
[1] "1 Olympic gold medalists are from Israel."
[1] "575 Olympic gold medalists are from Italy."
[1] "1 Olympic gold medalists are from Ivory Coast."
[1] "38 Olympic gold medalists are from Jamaica."
[1] "247 Olympic gold medalists are from Japan."
[1] "1 Olympic gold medalists are from Jordan."
[1] "20 Olympic gold medalists are from Kazakhstan."
[1] "34 Olympic gold medalists are from Kenya."
[1] "1 Olympic gold medalists are from Kosovo."
[1] "3 Olympic gold medalists are from Latvia."
[1] "2 Olympic gold medalists are from Liechtenstein."
[1] "6 Olympic gold medalists are from Lithuania."
[1] "4 Olympic gold medalists are from Luxembourg."
[1] "30 Olympic gold medalists are from Mexico."
[1] "2 Olympic gold medalists are from Mongolia."
[1] "6 Olympic gold medalists are from Morocco."
[1] "1 Olympic gold medalists are from Mozambique."
[1] "1 Olympic gold medalists are from Nepal."
[1] "287 Olympic gold medalists are from Netherlands."
[1] "90 Olympic gold medalists are from New Zealand."
[1] "23 Olympic gold medalists are from Nigeria."
[1] "16 Olympic gold medalists are from North Korea."
[1] "378 Olympic gold medalists are from Norway."
[1] "42 Olympic gold medalists are from Pakistan."
[1] "1 Olympic gold medalists are from Panama."
[1] "1 Olympic gold medalists are from Peru."
[1] "117 Olympic gold medalists are from Poland."
[1] "4 Olympic gold medalists are from Portugal."
[1] "1 Olympic gold medalists are from Puerto Rico."
[1] "161 Olympic gold medalists are from Romania."
[1] "1599 Olympic gold medalists are from Russia."
[1] "157 Olympic gold medalists are from Serbia."
[1] "15 Olympic gold medalists are from Slovakia."
[1] "8 Olympic gold medalists are from Slovenia."
[1] "32 Olympic gold medalists are from South Africa."
[1] "221 Olympic gold medalists are from South Korea."
[1] "110 Olympic gold medalists are from Spain."
[1] "1 Olympic gold medalists are from Suriname."
[1] "479 Olympic gold medalists are from Sweden."
[1] "175 Olympic gold medalists are from Switzerland."
[1] "1 Olympic gold medalists are from Syria."
[1] "3 Olympic gold medalists are from Taiwan."
[1] "1 Olympic gold medalists are from Tajikistan."
[1] "9 Olympic gold medalists are from Thailand."
[1] "7 Olympic gold medalists are from Trinidad."
[1] "3 Olympic gold medalists are from Tunisia."
[1] "40 Olympic gold medalists are from Turkey."
[1] "678 Olympic gold medalists are from UK."
[1] "2638 Olympic gold medalists are from USA."
[1] "2 Olympic gold medalists are from Uganda."
[1] "47 Olympic gold medalists are from Ukraine."
[1] "1 Olympic gold medalists are from United Arab Emirates."
[1] "31 Olympic gold medalists are from Uruguay."
[1] "10 Olympic gold medalists are from Uzbekistan."
[1] "2 Olympic gold medalists are from Venezuela."
[1] "1 Olympic gold medalists are from Vietnam."
[1] "17 Olympic gold medalists are from Zimbabwe."

In this example we don’t loop over some numbers, but over the unique regions in our medal_counts data frame:

unique(medal_counts$Region)
 [1] "Algeria"                     "Argentina"                  
 [3] "Armenia"                     "Australia"                  
 [5] "Austria"                     "Azerbaijan"                 
 [7] "Bahamas"                     "Bahrain"                    
 [9] "Belarus"                     "Belgium"                    
[11] "Brazil"                      "Bulgaria"                   
[13] "Burundi"                     "Cameroon"                   
[15] "Canada"                      "Chile"                      
[17] "China"                       "Colombia"                   
[19] "Costa Rica"                  "Croatia"                    
[21] "Cuba"                        "Czech Republic"             
[23] "Denmark"                     "Dominican Republic"         
[25] "Ecuador"                     "Egypt"                      
[27] "Estonia"                     "Ethiopia"                   
[29] "Fiji"                        "Finland"                    
[31] "France"                      "Georgia"                    
[33] "Germany"                     "Greece"                     
[35] "Grenada"                     "Haiti"                      
[37] "Hungary"                     "India"                      
[39] "Individual Olympic Athletes" "Indonesia"                  
[41] "Iran"                        "Ireland"                    
[43] "Israel"                      "Italy"                      
[45] "Ivory Coast"                 "Jamaica"                    
[47] "Japan"                       "Jordan"                     
[49] "Kazakhstan"                  "Kenya"                      
[51] "Kosovo"                      "Latvia"                     
[53] "Liechtenstein"               "Lithuania"                  
[55] "Luxembourg"                  "Mexico"                     
[57] "Mongolia"                    "Morocco"                    
[59] "Mozambique"                  "Nepal"                      
[61] "Netherlands"                 "New Zealand"                
[63] "Nigeria"                     "North Korea"                
[65] "Norway"                      "Pakistan"                   
[67] "Panama"                      "Peru"                       
[69] "Poland"                      "Portugal"                   
[71] "Puerto Rico"                 "Romania"                    
[73] "Russia"                      "Serbia"                     
[75] "Slovakia"                    "Slovenia"                   
[77] "South Africa"                "South Korea"                
[79] "Spain"                       "Suriname"                   
[81] "Sweden"                      "Switzerland"                
[83] "Syria"                       "Taiwan"                     
[85] "Tajikistan"                  "Thailand"                   
[87] "Trinidad"                    "Tunisia"                    
[89] "Turkey"                      "UK"                         
[91] "USA"                         "Uganda"                     
[93] "Ukraine"                     "United Arab Emirates"       
[95] "Uruguay"                     "Uzbekistan"                 
[97] "Venezuela"                   "Vietnam"                    
[99] "Zimbabwe"                   

i takes each of these elements in turn, then the corresponding n value of each country gets extracted and pasted into some sentence. In this case, i takes the value Algeria in the first iteration, and Zimbabwe in the last iteration.

Tip

It should be noted that in general it is good practice to try avoiding for loops and use functions from the apply-family instead, as for-loops allow you to write horrible code. Still, sometimes for-loops are the better option, depending on the use case. Also, their structure is pretty much the same over many programming languages.

Conditions

If-else statement

Have you noticed that our output doesn’t sound that correct if we only have one gold medal winner in a country?

"1 Olympic gold medalists are from Mozambique."

To solve this, we can add a conditional statement which only gets executed if some condition is met. This can be done by an if else statement:

for(i in unique(medal_counts$Region)){
  n_medals <- medal_counts[medal_counts$Region == i, "n"]
  
  ## Execute only if the number of medal is equal to 1!
  if(n_medals == 1){
    print(
    paste0(
      "One Olympic gold medalist is from ", 
      i, 
      "." )
    )
  }else{
    ## In all other cases, do the following:
  print(
    paste0(
      n_medals, 
      " Olympic gold medalists are from ", 
      i, 
      "." )
    )
  }
}
[1] "5 Olympic gold medalists are from Algeria."
[1] "91 Olympic gold medalists are from Argentina."
[1] "2 Olympic gold medalists are from Armenia."
[1] "368 Olympic gold medalists are from Australia."
[1] "108 Olympic gold medalists are from Austria."
[1] "7 Olympic gold medalists are from Azerbaijan."
[1] "14 Olympic gold medalists are from Bahamas."
[1] "One Olympic gold medalist is from Bahrain."
[1] "24 Olympic gold medalists are from Belarus."
[1] "98 Olympic gold medalists are from Belgium."
[1] "109 Olympic gold medalists are from Brazil."
[1] "54 Olympic gold medalists are from Bulgaria."
[1] "One Olympic gold medalist is from Burundi."
[1] "20 Olympic gold medalists are from Cameroon."
[1] "463 Olympic gold medalists are from Canada."
[1] "3 Olympic gold medalists are from Chile."
[1] "351 Olympic gold medalists are from China."
[1] "5 Olympic gold medalists are from Colombia."
[1] "One Olympic gold medalist is from Costa Rica."
[1] "58 Olympic gold medalists are from Croatia."
[1] "164 Olympic gold medalists are from Cuba."
[1] "123 Olympic gold medalists are from Czech Republic."
[1] "179 Olympic gold medalists are from Denmark."
[1] "3 Olympic gold medalists are from Dominican Republic."
[1] "One Olympic gold medalist is from Ecuador."
[1] "7 Olympic gold medalists are from Egypt."
[1] "13 Olympic gold medalists are from Estonia."
[1] "22 Olympic gold medalists are from Ethiopia."
[1] "13 Olympic gold medalists are from Fiji."
[1] "198 Olympic gold medalists are from Finland."
[1] "501 Olympic gold medalists are from France."
[1] "8 Olympic gold medalists are from Georgia."
[1] "1301 Olympic gold medalists are from Germany."
[1] "62 Olympic gold medalists are from Greece."
[1] "One Olympic gold medalist is from Grenada."
[1] "One Olympic gold medalist is from Haiti."
[1] "432 Olympic gold medalists are from Hungary."
[1] "138 Olympic gold medalists are from India."
[1] "One Olympic gold medalist is from Individual Olympic Athletes."
[1] "11 Olympic gold medalists are from Indonesia."
[1] "18 Olympic gold medalists are from Iran."
[1] "9 Olympic gold medalists are from Ireland."
[1] "One Olympic gold medalist is from Israel."
[1] "575 Olympic gold medalists are from Italy."
[1] "One Olympic gold medalist is from Ivory Coast."
[1] "38 Olympic gold medalists are from Jamaica."
[1] "247 Olympic gold medalists are from Japan."
[1] "One Olympic gold medalist is from Jordan."
[1] "20 Olympic gold medalists are from Kazakhstan."
[1] "34 Olympic gold medalists are from Kenya."
[1] "One Olympic gold medalist is from Kosovo."
[1] "3 Olympic gold medalists are from Latvia."
[1] "2 Olympic gold medalists are from Liechtenstein."
[1] "6 Olympic gold medalists are from Lithuania."
[1] "4 Olympic gold medalists are from Luxembourg."
[1] "30 Olympic gold medalists are from Mexico."
[1] "2 Olympic gold medalists are from Mongolia."
[1] "6 Olympic gold medalists are from Morocco."
[1] "One Olympic gold medalist is from Mozambique."
[1] "One Olympic gold medalist is from Nepal."
[1] "287 Olympic gold medalists are from Netherlands."
[1] "90 Olympic gold medalists are from New Zealand."
[1] "23 Olympic gold medalists are from Nigeria."
[1] "16 Olympic gold medalists are from North Korea."
[1] "378 Olympic gold medalists are from Norway."
[1] "42 Olympic gold medalists are from Pakistan."
[1] "One Olympic gold medalist is from Panama."
[1] "One Olympic gold medalist is from Peru."
[1] "117 Olympic gold medalists are from Poland."
[1] "4 Olympic gold medalists are from Portugal."
[1] "One Olympic gold medalist is from Puerto Rico."
[1] "161 Olympic gold medalists are from Romania."
[1] "1599 Olympic gold medalists are from Russia."
[1] "157 Olympic gold medalists are from Serbia."
[1] "15 Olympic gold medalists are from Slovakia."
[1] "8 Olympic gold medalists are from Slovenia."
[1] "32 Olympic gold medalists are from South Africa."
[1] "221 Olympic gold medalists are from South Korea."
[1] "110 Olympic gold medalists are from Spain."
[1] "One Olympic gold medalist is from Suriname."
[1] "479 Olympic gold medalists are from Sweden."
[1] "175 Olympic gold medalists are from Switzerland."
[1] "One Olympic gold medalist is from Syria."
[1] "3 Olympic gold medalists are from Taiwan."
[1] "One Olympic gold medalist is from Tajikistan."
[1] "9 Olympic gold medalists are from Thailand."
[1] "7 Olympic gold medalists are from Trinidad."
[1] "3 Olympic gold medalists are from Tunisia."
[1] "40 Olympic gold medalists are from Turkey."
[1] "678 Olympic gold medalists are from UK."
[1] "2638 Olympic gold medalists are from USA."
[1] "2 Olympic gold medalists are from Uganda."
[1] "47 Olympic gold medalists are from Ukraine."
[1] "One Olympic gold medalist is from United Arab Emirates."
[1] "31 Olympic gold medalists are from Uruguay."
[1] "10 Olympic gold medalists are from Uzbekistan."
[1] "2 Olympic gold medalists are from Venezuela."
[1] "One Olympic gold medalist is from Vietnam."
[1] "17 Olympic gold medalists are from Zimbabwe."

Let’s look at a more simple example to explain the concept:

if(condition){
  do something
}else{
  do something else
}

For example:

x <- 10

if(x < 100){
  x * 2
}else{
  x
}
[1] 20

Inside the if() we define our condition. Then, inside the { } we define an operation that gets executed, if this condition is met. Inside the else{ } part we define an operation that gets executed if the condition in if() is not met.

In our inital example we check if the number of gold medalists is equal to one. If that is the case, we print the specific statement. If not, we print the other.

We don’t even need to define the else{} part if nothing should happen in case the if() condition is not met.

ifelse()

We can use this concept for adding new values conditionally to a data frame. For example, let’s build a dichotomous variable to check which countries have equal to or more than 100 gold medal winners:

medal_counts$n_100 <- ifelse(
 medal_counts$n >= 100,
  yes = TRUE,
  no = FALSE
)

head(medal_counts)
# A tibble: 6 × 4
# Groups:   Region [6]
  Region     Medal     n n_100
  <chr>      <chr> <int> <lgl>
1 Algeria    Gold      5 FALSE
2 Argentina  Gold     91 FALSE
3 Armenia    Gold      2 FALSE
4 Australia  Gold    368 TRUE 
5 Austria    Gold    108 TRUE 
6 Azerbaijan Gold      7 FALSE

Note that the look of this ifelse() function is a bit different from our if else statement, but the logic behind it is exactly the same: Here we add the new column n_100 which gets filled with TRUE and FALSE. If the conditional statement medal_counts$n >= 100, which is the first argument of the ifelse() function, is met, the function returns a TRUE, if not a FALSE.

Footnotes

  1. Image by Tine Ivanic on Unsplash.↩︎