Get startedGet started for free

Mean carried forward

An alternative to last observation carried forward is to replace NAs with the mean of all the previous non-NA values. This is called mean carried forward. Again, R makes us choose between readability and speed. The following is written for readability:

na_meancf1 <- function(x) {
  total_not_na <- 0
  n_not_na <- 0
  res <- x
  for(i in seq_along(x)) {
    if(is.na(x[i])) {
      res[i] <- total_not_na / n_not_na
    } else {
      total_not_na <- total_not_na + x[i]
      n_not_na <- n_not_na + 1
    }
  }
  res
}

The iterative nature makes this tricky to vectorize, so instead, let's convert it to C++. Complete the definition of na_meancf2(), a C++ translation of na_meancf1().

This exercise is part of the course

Optimizing R Code with Rcpp

View Course

Exercise instructions

  • In the if condition, check if the ith element of x is a NumericVector's NA.
  • If the condition is true, set the ith result to the total of non-missing values, total_not_na, divided by the number of missing values, n_not_na.
  • Otherwise, increase total_not_na by the ith element of x, and add 1 to n_not_na.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

#include 
using namespace Rcpp; 

// [[Rcpp::export]]
NumericVector na_meancf2(NumericVector x) {
  double total_not_na = 0.0;
  double n_not_na = 0.0;
  NumericVector res = clone(x);
  
  int n = x.size();
  for(int i = 0; i < n; i++) {
    // If ith value of x is NA
    if(___) {
      // Set the ith result to the total of non-missing values 
      // divided by the number of non-missing values
      res[i] = ___ / ___;
    } else {
      // Add the ith value of x to the total of non-missing values
      ___;
      // Add 1 to the number of non-missing values
      ___;
    }
  }  
  return res;
}

/*** R
  library(microbenchmark)
  set.seed(42)
  x <- rnorm(1e5)
  x[sample(1e5, 100)] <- NA  
  microbenchmark( 
    na_meancf1(x), 
    na_meancf2(x), 
    times = 5
  )
*/
Edit and Run Code