linearly interpolate NA values in Rcpp::NumericVector I am trying to fill missing values with linearly interpolated values in an Rcpp::NumericVector -- leaving in place leading and trailing NA-values. 

The desired output is below: 

R&gt; x &lt;- c(NA, NA, NA, 1:4, NA, 6:8, NA, 10, NA, NA, NA)
R&gt; naLinInt(x)
R&gt; [1] NA NA NA  1  2  3  4  5  6  7  8  9 10 NA NA NA


I've implemented it via the below, but as i am very new to C++ i'd very much benefit from some help in understanding how to do this better. 

I feel that i've really hacked my way through this -- in particular i suspect i've over-used pointers and that there's a better way to do this sort of indexing and offsetting. 

#include&lt;Rcpp.h&gt;

//[[Rcpp::export]]
Rcpp::NumericVector naLinInt(Rcpp::NumericVector x) {
    // This function linearly interpolates between NA values

    Rcpp::Rcout &lt;&lt; "We begin with: " &lt;&lt; std::endl &lt;&lt; x &lt;&lt; std::endl;

    double * it1 = x.begin();
    double * it2 = x.begin();
    int step = 0;

    it1++;

    while( it1 &lt; x.end() ) {
        if( Rcpp::NumericVector::is_na(*it1)) {
            if( Rcpp::NumericVector::is_na(*(it1 - 1))) {
                it1++;
                continue;
            } else {
            it2 = it1;
            step = 0;
            while(it2 &lt; x.end() &amp;&amp; Rcpp::NumericVector::is_na(*it2)) {
                step++;
                it2++;
            }
            if( it2 == x.end()) break;
            // step through missing values and replace
            for(int j = 0; j &lt; step; j++) {
                *it1 = *(it1 - 1) + (*it2 - *(it1 - 1))/(step + 1 - j);
                it1++;
            }
            it1 = it2 + 1;
            }
        } else {
            it1++;
        }
    }

    return x;
}
As you suggest, the code does look very "pointery".  I think it might be made clearer with use of some standard algorithms:


std::adjacent_find() (with suitable predicate functions) to find a number followed by NA, or a NA followed by a number.
std::generate() (with suitable generator function) to populate a series of values from one iterator to another.


With those, you won't need any arithmetic on iterators other than a subtraction to find the number of elements you're interpreting.



The form of such a solution is something like this (untested):

#include &lt;Rcpp.h&gt;
#include &lt;algorithm&gt;

constexpr auto is_na = Rcpp::NumericVector::is_na;

//[[Rcpp::export]]
Rcpp::NumericVector naLinInt(Rcpp::NumericVector x) {
    // This function linearly interpolates to fill sequences of NA
    // values surrounded by valid numbers.
    static auto const detect_start_na = [](auto a, auto b){
        return !is_na(a) &amp;&amp; is_na(b);
    };
    static auto const detect_end_na = [](auto a, auto b){
        return is_na(a) &amp;&amp; !is_na(b);
    };

    auto start = x.begin();

    while (true) {
        // Find transitions to and from NA values.  If we hit end of
        // vector whilst looking, our work is done.
        auto num_to_na = std::adjacent_find(start, x.end(), detect_start_na);
        auto na_to_num = std::adjacent_find(start, x.end(), detect_end_na);
        if (na_to_num == x.end()) {
            break;
        }

        // At this point, num_to_na points to the last number before
        // an interpolation block, and na_to_num points to the last NA
        // of that block.

        ++na_to_num;            // Now, both iterators point to numbers.
        auto const base = *num_to_na;
        auto const target = *na_to_num;

        // To count rails rather than posts, measure difference before
        // incrementing the start position.
        auto const gaps = std::distance(na_to_num, num_to_na);

        ++num_to_na;
        // Now both iterators point immediately *after* transition.

        auto const make_value = [base, target, gaps, i = std::size_t{0}]()
            mutable { return base + (++i * (target - base) / gaps); };
        std::generate(na_to_num, num_to_na, make_value);

        // Advance onwards
        start = na_to_num;
    }

    return x;
}


There are a couple of iterator increments there (because std::adjacent_find returns an iterator to the first of the matching pair), but there's quite a lot less arithmetic than the original.    

Although this is longer than the original, much of the difference is the comments that help the reader understand how the state changes through the loop.  That's something that I think is an improvement (though perhaps the terms "rails" and "posts" require a link to a definition of fencepost error for the uninitiated).