Quiz: Functions

Week 2 · Advanced Statistical Programming using R

Recap: Git and Tidyverse

Q1

You edited a file called analysis.R and now want to save this change in your Git history. What is the correct order of commands?

  1. git commit -m "update"git add analysis.R
  2. git add analysis.Rgit commit -m "update"
  3. git initgit commit -m "update"
  4. git statusgit log

Answer: 2) git add stages the change, and git commit records it as a snapshot in the repository.

Q2

Which dplyr function would you use to keep only rows where species == "Adelie"?

  1. select(species == "Adelie")
  2. filter(species == "Adelie")
  3. mutate(species == "Adelie")
  4. summarise(species == "Adelie")

Answer: 2) filter() keeps rows that satisfy a condition.

Functions

Q3

What is the main purpose of writing a function?

  1. So we can increase our lines of code
  2. To avoid using packages
  3. To create a reusable block of code
  4. To prevent R from returning output

Answer: 3) functions package code that can be reused, tested and given a meaningful name.

Q4

Which part of this function definition is the function argument?

max_minus_min <- function(x) {
  max(x, na.rm = TRUE) - min(x, na.rm = TRUE)
}
  1. na.rm = TRUE
  2. function(x)
  3. max_minus_min
  4. x

Answer: 4) x is the input supplied to the functions

Q5

What does R return if a function has no explicit return() statement?

  1. Nothing
  2. NULL
  3. The first line of the function body
  4. The value of the last evaluated expression

Answer: 4) R automatically returns the value of the last evaluated expression. You can also use return() - useful for early exits or to make intent clearer.

Q6

What is a “silent error”?

  1. An error that prints a very long message
  2. A function that crashes immediately
  3. A function that returns the wrong output without producing an error
  4. A common warning message from ggplot2

Answer: 3) The code runs and returns an output. Silent errors may be overlooked.

Q7

What is the main problem of writing a function that does too many things at once?

  1. It is always slow
  2. Difficult to name, test, debug and reuse
  3. R function blocks can only handle up to 128 characters
  4. None, functions should be a jack of all trades

Answer: 2) Hard to test in isolation, debug and reuse. Good functions help with readability and maintainability.

Q8

We wrote a trader function that tells us what to do with our stocks.

recommend_stock <- function(price, buy_below, sell_above) {
  if (price < buy_below) {
    "buy"
  } else if (price > sell_above) {
    "sell"
  } else {
    "hold"
  }
}
recommend_stock(price = 120, buy_below = 100, sell_above = 150)

Which output would the function return?

  1. “buy”
  2. “hold”
  3. “sell”
  4. NULL

Answer: 2) The price is neither below buy_below nor above sell_above, so the function returns "hold".

Q9

The lecture extended the classic DRY rule (“Don’t Repeat Yourself”) to DRRY. What does the extra R stand for?

  1. “Really” - write a function whenever code feels important
  2. “Re-read” - re-read code 3 times to understand it, write (or improve) a function
  3. “Rewrite” - rewrite every function at least 3 times before committing
  4. “Reactive” - only write functions when errors occur

Answer: 2) DRY targets repetition in the text of your code. DRRY targets cognitive load - effort to understand your code later on.

Q10

What will this function output?

f <- function(vec) {
  len <- length(vec)
  if (len == 1) {
    vec[1]
  } else {
    vec[len] + f(vec[1:len-1])
  }
}
f(1:3)
  1. The first entry of vec
  2. The sum of the first entry of vec and the length of vec
  3. An error
  4. The sum of all entries of vec

Answer: 4) f is a recursive function adding the last item of vec to the sum of the rest.