Exercises

  1. We are given the following list:

    lst <- list(yabba = letters,
                dabba = list(x = LETTERS,
                             y = 1:10),
                do = bcscr::m111survey)

    One way to access the letter “b” in the first element of lst is as follows:

    lst$yabba[2]
    ## [1] "b"

    Another way is:

    lst[[1]][2]
    ## [1] "b"
    For each of the following objects, find at least two ways to access it within lst:
    • the vector of letters from “c” to “j”;
    • the capital letter “F”;
    • the vector of numbers from 1 to 10;
    • the heights of the five tallest individuals in m111survey.
  2. Write a function called goodStats() that, when given a vector of numerical values, computes the mean, median and standard deviation of the values, and returns these values in a list. The function should take two parameters:
    • x: the vector of numerical values;
    • ...: the ellipses, which allow the user to pass in additional arguments.
    The list returned should name each of the three quantities:
    • the name of the mean should be mean;
    • the name of the standard deviation should be sd;
    • the name of the median should be median.

    Typical examples of use should look like this:

    vec <- 1:5
    goodStats(x = vec)
    ## $mean
    ## [1] 3
    ## 
    ## $sd
    ## [1] 1.581139
    ## 
    ## $median
    ## [1] 3
    vec <- c(3, 7, 9, 11, NA)
    myStats <- goodStats(x = vec, na.rm = TRUE)
    myStats$mean
    ## [1] 7.5