Loops and control statements


Ex. 4.1

What is the value of i after the execution of the R code below? Try to answer before running the code in R.

y <- c(-1, 100, 2, 4)

for (i in 1:length(y)) {
  
  if (y[i] == 100) {
    
    break
    
  }
}


Ex. 4.2

What is the value of i after the execution of the R code below? Try to answer before running the code in R.

j <- 0
x <- c(0, -1, 3, -4, 4, 2)

for (i in 1:length(x)) {
  
  if (x[i] > 0) {
    
    j <- j + x[i]
    
  }
}


Ex. 4.3

What is the value the warn variable after the execution of the R code below? Try to answer before running the code in R.

glycemia <- 120 # mg/dL

hypo_thresh <- 70 # mg/dL
hyper_thresh <- 130 # mg/dL

warn <-NA

if ( glycemia < hypo_thresh ) {
  
  warn <- "Hypoglycemia"
  
} else if ( glycemia > hyper_thresh ) {
  
  warn <- "Hyperglycemia"
  
} else {
  
  warn <- "Euglycemia"
  
}


Ex. 4.4

Write a function that takes in input a temperature in Fahrenheit, converts it into Celsius (see Ex. 2.11) and, if the temperature is equal or lower than 0°C, prints to screen “freeze” using the cat function.

Test the function on the following values: 25°F, 32°F, and 68°F.


Ex. 4.5

Load the built-in R dataset called iris using the following instruction

data("iris")

The iris dataset is saved in a data.frame that contains the measurements in centimeters of sepal length, sepal width, petal length, and petal width, for flowers from three species of iris: Iris setosa, versicolor, and virginica.

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

In a script, initialize two variables, N and S, to 0.

These variables will be used to save the number of flowers from the versicolor species (N) and the sum of their petal lengths (S).

Use a for loop to iterate though the rows of iris and, at each iteration, if the corresponding flower belongs to the versicolor species:

  • Add 1 to the N variable
  • Add the value of Sepal.Length to S

Compute the mean sepal length for the Iris versicolor species as \(S/N\).

Verify that this value matches the results obtained by directly computing the mean on the Sepal.Length after extracting only the rows corresponding to the flowers from the Iris versicolor species.


Ex. 4.6

Initialize to -Inf a vector called PetLen.setosa.max.

Write a script that iterates through the the rows of the iris data.frame and if the considered flower belongs to the Iris setosa species and its petal length is longer than the one saved in PetLen.setosa.max, then it updates PetLen.setosa.max with the current petal length.

Verify that this value matches the results obtained by directly computing the maximum on the Petal.Length after extracting only the rows corresponding to the flowers from the Iris setosa species.


Ex. 4.7

Initialize the following list and vector:

SepWid <- list( setosa=0, versicolor=0, virginica=0)
thresh <- 3

You will save in the SepWid list the number of flowers for each species that have a sepal width greater than 3 cm.

To do so, write a script that iterates through the the rows of the iris data.frame and update the counts in SepWid whenever Sepal.Width is greater than thresh.

Tip: use the if/else statement to consider the counts for the various species separately.

Ex. 4.8

Modify the script of Ex. 4.7 to run the same analysis without using the if/else statement but only a single if (to check whether the sepal width is higher than the threshold). Save the results in a new list called SepWid2.

Ex. 4.9

Write a new script to perform the same analysis of Ex. 4.7 and 4.8 but by implementing the following steps:

  • Select only the rows in the iris data.frame corresponding to a sepal width greater than the threshold and save it into a new data.frame.

  • Use the table function to compute the occurrences of the three iris species.