Created
April 27, 2016 06:39
-
-
Save badbye/c574c69f177eedf4faf7086dfd0ff36f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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