5 Control flow

5.1 Choices

  1. Q: What type of vector does each of the following calls to ifelse() return?

    Read the documentation and write down the rules in your own words.

    A: The arguments of ifelse() are test, yes and no. ifelse() is vectorised, so when yes or no are shorter than test, they will be recycled. (When they are longer than test, their additional elements will be ignored.)

    The documentation says:

    A vector of the same length and attributes (including dimensions and “class”) as test and data values from the values of yes or no. The mode of the answer will be coerced from logical to accommodate first any values taken from yes and then any values taken from no.

    This surprising because it uses the type of test

    The function returns the entry for yes when test is TRUE, no when test is FALSE or NA when test is NA. Therefore, the expressions above return vectors of type double, character and logical.

  2. Q: Why does the following code work?

    A: if() expects a logical condition, but also accepts a numeric vector where 0 is treated as FALSE and all other numbers are treated as TRUE. Numeric missing values (including NaN) lead to an error in the same way that a logical missing, NA, does.

5.2 Loops

  1. Q: Why does this code succeed without errors or warnings?

    A: The vector we are iterating over are of length 0. Our loop goes from i = 1 to i = 0 because : counts down, as well as up. As we use [<- and [ for indexing, we need to be aware of their subsetting behaviour for out-of-bounds and zero indices.

    During the first iteration x[1] will generate an NA (out-of-bounds indexing for atomics). The resulting NA (from squaring) will be assigned to the empty length-1 list out[1] (out-of-bounds indexing for lists).

    In the next iteration, x[0] will return numeric(0) (zero indexing for atomics). Again squaring doesn’t change the value and numeric(0) is assigned to out[0] (zero indexing for lists). Assigning a 0-length vector to a 0-length subset works, but doesn’t change anything.

    Overall the code works, because each step includes valid R operations. (Though it may have been very helpful to warn the user about the unusual inputs to this loop.)

  2. Q: What does the following code tell you about when the vector being iterated over is evaluated?

    A: In this loop x takes on the values of the initial xs (1, 2 and 3), indicating that it is evaluated just once in the beginning of the loop, not after each iteration.

  3. Q: What does the following code tell you about when the index is updated?

    A: In a for-loop the index is updated in the beginning of each iteration. Therefore, reassigning the index symbol during one iteration doesn’t affect the following iterations.