I am writing a cpp function to replace any NA values with the next non-na value. Code works properly regarding the replacement, however I want to return back the NA values for those that don't have a later non-NA value.
Eg:
fill_backward(c(1, NA, 2))
--> 1, 2, 2
fill_backward(c(1, NA, 2, NA))
--> 1, 2, 2, NA
#include <Rcpp.h>
using namespace Rcpp;
//' given NA values fill them with the next non-na value
//' @param x A numeric vector of values
//' @details
//' Works very well in context of dplyr to carry out last-observation-carried-foward
//' for different individuals. It will NOT replace leading NA's
//' @examples /dontrun {
//' fill_forward(c(1.0, NA, 2))
//' fill_forward(c(NA, 1, NA, 2))
//' library(dplyr)
//' df <- data_frame(id = c(1, 1, 2, 2), obs = c(1.2, 4.8, 2.5, NA))
//' df %>% group_by(id) %>% mutate(obs_locf = fill_forward(obs))
//' }
//' @export
// [[Rcpp::export]]
NumericVector fill_backward(NumericVector x) {
int n = x.size();
NumericVector out = no_init(n);
for (int i = 0; i < n; ++i) {
if (R_IsNA(x[i])) {
for (int j = i+1; j < n; ++j) {
if(R_IsNA(x[j])) {
continue;
} else {
out[i] = x[j];
break;
}
//if never gets to another actual value
out[i] = NumericVector::get_na();
}
} else { //not NA
out[i] = x[i];
}
}
return out;
}
Currently fill_backward(c(NA, 1.0, NA, 2, NA, NA))
returns:
[1] 1.000000e+00 1.000000e+00 2.000000e+00
[4] 2.000000e+00 2.156480e-314 -1.060998e-314
instead of 1 1 2 2 NA NA
For returning the NA value back it is out[i] = NumericVector::get_na();
I have also tried out[i] = REAL_NA
and out[i] = x[i]` and nothing seems to work.
Finally, I used the same type of implementation for a fill_forward implementation, which can be seen here where leading NA's should return as NA
- and it properly returns NA values so I am at a complete loss.
EDIT: Fixed thanks to @Roland 's suggestions