calc_ranks | R Documentation |
RcppArmadillo
.Calculate the ranks of the elements of a single-column time series,
matrix, or a vector using RcppArmadillo
.
calc_ranks(tseries)
tseries |
A single-column time series, matrix, or a vector. |
The function calc_ranks()
calculates the ranks of the elements of a
single-column time series, matrix, or a vector.
The permutation index is an integer vector which sorts a given vector into
ascending order.
The permutation index of the permutation index is the reverse
permutation index, because it sorts the vector from ascending order back
into its original unsorted order.
The ranks of the elements are equal to the reverse permutation
index. The function calc_ranks()
calculates the reverse
permutation index.
The ranks produced by calc_ranks()
start at zero, following the
C++
convention.
The Armadillo
function arma::sort_index()
calculates the
permutation index which sorts a given vector into an 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 function calc_ranks()
calls the Armadillo
function
arma::sort_index()
twice to calculate the reverse
permutation index, to sort the vector from ascending order back into its
original unsorted order.
An integer vector with the ranks of the elements of the
tseries
.
## Not run:
# Create a vector of data
datav <- rnorm(1e3)
# Calculate the ranks of the elements using R code and RcppArmadillo
all.equal(rank(datav), drop(HighFreq::calc_ranks(datav))+1)
# Compare the speed of R code with RcppArmadillo
library(microbenchmark)
summary(microbenchmark(
Rcode=rank(datav),
Rcpp=calc_ranks(datav),
times=10))[, c(1, 4, 5)] # end microbenchmark summary
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.