R/row_rank.R

Defines functions row_rank has_rank

Documented in has_rank row_rank

#' 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
}
greatgray/scorecard documentation built on May 17, 2019, 8:34 a.m.