Here is a solution in pure Armadillo code, using the function arma::sort_index()
.
The function arma::sort_index()
calculates the permutation index to sort a given vector into ascending order.
Applying the function arma::sort_index()
twice:
arma::sort_index(arma::sort_index())}
, calculates the reverse permutation index to sort the vector from ascending order back into its original unsorted order. The ranks of the elements are equal to the reverse permutation index.
Below is Armadillo code wrapped in RcppArmadillo that defines the function calc_ranks()
. The function calc_ranks()
calculates the ranks of the elements of a vector, and it can be called from R.
#include <RcppArmadillo.h>
using namespace Rcpp;
using namespace arma;
// [[Rcpp::depends(RcppArmadillo)]]
// Define the function calc_ranks(), to calculate
// the ranks of the elements of a vector.
//
// [[Rcpp::export]]
arma::uvec calc_ranks(const arma::vec& da_ta) {
return (arma::sort_index(arma::sort_index(da_ta)) + 1);
} // end calc_ranks
The above code can be saved to the file calc_ranks.cpp, so it can be compiled in R using the function Rcpp::sourceCpp()
.
Below is R code to test the function calc_ranks()
(after it's been compiled in R):
# Compile Rcpp functions
Rcpp::sourceCpp(file="C:/Develop/R/Rcpp/calc_ranks.cpp")
# Create a vector of random data
da_ta <- runif(7)
# Calculate the ranks of the elements
calc_ranks(da_ta)
# Compare with the R function rank()
all.equal(rank(da_ta), drop(calc_ranks(da_ta)))