Exercise

Vector cloning

Unlike R, C++ uses a copy by reference system, meaning that if you copy a variable then make changes to the copy, the changes will also take place in the original.

// [[Rcpp::export]]
NumericVector always_returns_two(NumericVector x) {
  // Make a copy
  NumericVector y = x;
  // Modify the copy
  y[0] = 2;
  // The changes also happen in the original
  return x[0];
}

To prevent this behavior, you have to use the clone() function to copy the underlying data from the original variable into the new variable. The syntax is y = clone(x). In this exercise, we have defined two functions for you:

  • change_negatives_to_zero(): Takes a numeric vector, modifies by replacing negative numbers with zero, then returns both the original vector and the copy.
  • change_negatives_to_zero_with_cloning(): Does the same thing as above, but clones the original vector before modifying it.

Instructions

100 XP
  • Complete the function definition of change_negatives_to_zero() by setting the_original to the_copy.
  • Complete the function definition of change_negatives_to_zero_with_cloning() by setting the_copy to the clone of the_original.
  • Read the contents of the console to compare the output of each function.