Created
October 16, 2018 20:21
-
-
Save slwu89/2568754aafb0e47f326d1817df7272d8 to your computer and use it in GitHub Desktop.
useful functions in Rcpp
This file contains hidden or 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
/* the "vals" part of R's rle (run-length encoding) */ | |
Rcpp::IntegerVector rle_vals(const Rcpp::IntegerVector& x){ | |
int n = x.size(); | |
/* y */ | |
Rcpp::IntegerVector head = x[Rcpp::seq(1,n-1)]; | |
Rcpp::IntegerVector tail = x[Rcpp::seq(0,n-2)]; | |
Rcpp::LogicalVector y = head != tail; | |
/* i */ | |
Rcpp::IntegerVector y_ix = Rcpp::seq(0,y.size()-1); | |
y_ix = y_ix[y]; | |
y_ix.push_back(n-1); | |
return x[y_ix]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment