Chapter 19

Textbook 19.3.1

Problem 1

Textbook 19.4.4

Problem 1, 6

Textbook 19.5.5

Problem 4

Chapter 20

Textbook 20.3.5

Problem 2

Textbook 20.5.4

Problem 1 (scan your drawing and link it with your .Rmd file)

Textbook 20.7.4

Problem 2, 3

Chapter 21

Textbook 21.2.1

Problem 2

Textbook 21.3.5

Problem 3

Textbook 21.4.1

Problem 1

Textbook 21.5.3

Problem 1

Textbook 21.9.3

Problem 3

Chapter 23

Textbook 23.4.5

Problem 3

Chapter 24

Textbook 24.2.3

Problem 2

Textbook 24.3.5

Problem 2

Extra question(s)

  1. (Data manipulation)

    1. For the R built-in iris dataset, the following R code is given:
     data(iris)
     swidth.versicolor <- NULL
     for (i in seq_len(nrow(iris)) ) {
             if (iris$Species[i]=="versicolor") {
                     swidth.versicolor <- c(swidth.versicolor, iris$Sepal.Width[i])
             }
     }

    Vectorize the above code.

    1. Type and run the following R code.
     data(iris)
     head(iris$Sepal.Length[iris$Species])

    Explain why you get those results.

  2. (Closure) Simply speaking, a closure is a function created by another function.

    1. Guess the output of the following code and explain why you get those results.
     power <- function(exponent) {
       function(x) {
         x ^ exponent
       }
     }
    
     square <- power(2)
     square(2)
     square(4)
    
     cube <- power(3)
     cube(2)
     cube(4)
    1. What do you think the call k() will return? Explain.
     j <- function(x) {
       y <- 2
       function() {
         c(x, y)
       }
     }
     k <- j(1)