Skip to content

Instantly share code, notes, and snippets.

@slwu89
Created October 16, 2018 20:21
Show Gist options
  • Save slwu89/2568754aafb0e47f326d1817df7272d8 to your computer and use it in GitHub Desktop.
Save slwu89/2568754aafb0e47f326d1817df7272d8 to your computer and use it in GitHub Desktop.
useful functions in Rcpp
/* 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