Skip to content

Instantly share code, notes, and snippets.

@badbye
Created April 27, 2016 06:39
Show Gist options
  • Save badbye/c574c69f177eedf4faf7086dfd0ff36f to your computer and use it in GitHub Desktop.
Save badbye/c574c69f177eedf4faf7086dfd0ff36f to your computer and use it in GitHub Desktop.
library(Rcpp)
cppFunction('NumericVector replace_na(NumericVector x) {
int n = x.size();
NumericVector out(n);
int temp = 0;
for(int i = 0; i < n; ++i) {
if (R_IsNA(x[i])){
out[i] = temp;
}else{
temp = x[i];
out[i] = x[i];
}
}
return out;
}')
a <-c(1,2,3,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,4,5,NA,6,7,8)
a = rep(a, 1000000)
system.time(d <- replace_na(a))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment