Learning Objectives

Rcpp Vectors

Creating Vectors

Accessing Elements

Rcpp Vector Methods

For-loops

Aliasing

Missing Values and Infinity

Exercises

  1. (from Advanced R) For each of the following functions, read the code and figure out what the corresponding base R function is. You might not understand every part of the code yet, but you should be able to figure out the basics of what the function does.

    C++

    double f1(NumericVector x) {
      int n = x.size();
      double y = 0;
    
      for(int i = 0; i < n; ++i) {
        y += x[i] / n;
      }
      return y;
    }

    C++

    NumericVector f2(NumericVector x) {
      int n = x.size();
      NumericVector out(n);
    
      out[0] = x[0];
      for(int i = 1; i < n; ++i) {
        out[i] = out[i - 1] + x[i];
      }
      return out;
    }

    C++

    bool f3(LogicalVector x) {
      int n = x.size();
    
      for(int i = 0; i < n; ++i) {
        if (x[i]) return true;
      }
      return false;
    }

    C++

    int f4(Function pred, List x) {
      int n = x.size();
    
      for(int i = 0; i < n; ++i) {
        LogicalVector res = pred(x[i]);
        if (res[0]) return i + 1;
      }
      return 0;
    }

    C++

    NumericVector f5(NumericVector x, NumericVector y) {
      int n = std::max(x.size(), y.size());
      NumericVector x1 = rep_len(x, n);
      NumericVector y1 = rep_len(y, n);
    
      NumericVector out(n);
    
      for (int i = 0; i < n; ++i) {
        out[i] = std::min(x1[i], y1[i]);
      }
    
      return out;
    }
  2. Create a function in C++ call fib() that will take as input n and return the first n Fibonacci numbers, where the sequence begins with 0, 1, 1, 2, 3, 5, 8, 13, …

    E.g.

    R

    fib(1)
    ## [1] 0
    fib(2)
    ## [1] 0 1
    fib(10)
    ##  [1]  0  1  1  2  3  5  8 13 21 34
  3. (from Advanced R) Convert the following functions into C++. For now, assume the inputs have no missing values. Try not to use Rcpp Sugar (e.g. Rcpp::min()).

    1. all().
    2. cumprod(), cummin(), cummax().
    3. diff(). Start by assuming lag 1, and then generalize for lag n.
    4. range().
    • all_cpp() output examples:

      R

      all_cpp(c(TRUE, TRUE, FALSE))
      ## [1] FALSE
      all_cpp(TRUE)
      ## [1] TRUE
      all_cpp(FALSE)
      ## [1] FALSE
      all_cpp(c(TRUE, TRUE))
      ## [1] TRUE
    • cumprod_cpp() output examples

      R

      x <- 1:4
      cumprod_cpp(x)
      ## [1]  1  2  6 24
      x <- double()
      cumprod_cpp(x)
      ## numeric(0)
      x <- 3
      cumprod_cpp(x)
      ## [1] 3
    • cummin_cpp() output examples:

      R

      cummin_cpp(double())
      ## numeric(0)
      cummin_cpp(3)
      ## [1] 3
      cummin_cpp(c(10, 11, 3, 4, 5, 1, 2))
      ## [1] 10 10  3  3  3  1  1
    • cummax_cpp() output examples:

      R

      cummax_cpp(double())
      ## numeric(0)
      cummax_cpp(3)
      ## [1] 3
      cummax_cpp(c(10, 11, 3, 4, 5, 22, 2))
      ## [1] 10 11 11 11 11 22 22
    • diff_cpp() output examples:

      R

      diff_cpp(x = c(1, 5, 10, 20), lag = 1)
      ## [1]  4  5 10
      diff_cpp(x = c(1, 5, 10, 20), lag = 2)
      ## [1]  9 15
      diff_cpp(x = c(1, 5, 10, 20), lag = 3)
      ## [1] 19
      diff_cpp(x = c(1, 5, 10, 20), lag = 4)
      ## numeric(0)
    • range_cpp() output examples:

      R

      range_cpp(c(9, 11, 22, 12, 1))
      ## [1]  1 22
      range_cpp(8)
      ## [1] 8 8
      range_cpp(double())
      ## [1] -Inf  Inf

National Science Foundation Logo American University Logo Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.