Session Ready
Exercise

Handling of missing values

In R, to test for a missing value you must use is.na(x). (What happens if you try x == NA?)

The equivalent in Rcpp is the static method is_na(). Recall that static means the method is part of the class, not the particular variable. For example, NumericVector::is_na(x) tests if the double x is a missing value.
Similarly, the static method get_na() gives you the NA for the associated class. For example, CharacterVector::get_na() returns a missing character value.

Note that the logical or in C++ is the same as in R, ||.

Instructions
100 XP
  • Update the weighted_mean_cpp() function from the previous exercise so that it returns a missing value as soon as a missing value is seen on x or w.
    • Add an if block that checks if the ith element of x is NA or the ith element of w is NA.
    • Inside that if block, return a numeric NA.