Skip to content

Instantly share code, notes, and snippets.

@artemklevtsov
Created April 14, 2018 09:07
Show Gist options
  • Save artemklevtsov/3d27e90e9285364f08e0b1b45b087aea to your computer and use it in GitHub Desktop.
Save artemklevtsov/3d27e90e9285364f08e0b1b45b087aea to your computer and use it in GitHub Desktop.
// [[Rcpp::plugins(cpp11)]]
#include <Rcpp.h>
using namespace Rcpp;
template <int RTYPE>
Vector<RTYPE> na_locf_impl(Vector<RTYPE> x) {
std::size_t n = x.size();
auto v = x[0];
for (std::size_t i = 0; i < n; ++i) {
if (Vector<RTYPE>::is_na(x[i]))
x[i] = v;
else
v = x[i];
}
return x;
}
// [[Rcpp::export]]
SEXP na_locf(SEXP x) {
RCPP_RETURN_VECTOR(na_locf_impl, x);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment