#' Computes the row-wise rank for each row in an XTS object.
#' The largest value in the row receives rank 1.
#' @param x the XTS object for row-wise ranking; must have more
#' than one column.
#' @return an XTS object of ranks with same columns as \code{x}
#' @examples
#' \dontrun{
#' library(quantmod)
#' getSymbols(c("XLP","XLV"))
#' roc <- cbind(ROC(Cl(XLP)),ROC(Cl(XLV)))
#' rv <- row_rank(roc)
#' head(rv)
#' }
row_rank <- function(x){
xts::as.xts(t(apply(-x, 1, rank, na.last = "keep")))
}
#' Whether an XTS object has a rank column
#' @param x the XTS object for row-wise ranking
#' @param which whether maching integer zero, default FALSE
#' @param ignore.case whether to ignore string case, default TRUE
#' @return TRUE if grep finds the word Rank in column names, ignoring case, FALSE otherwise
has_rank <- function(x, which = FALSE, ignore.case = TRUE) {
loc <- grep("Rank", colnames(x), ignore.case )
if (!identical(loc, integer(0))) {
return(if (which) loc else TRUE)
}
else FALSE
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.