R/rank_integer_index.R

Defines functions rank_integer_index

Documented in rank_integer_index

#' rank_integer_index
#'
#' Converts \code{rank_cols} in all allowed forms to an integer index. Takes in ranked cols, either given by integer, logical, or named index. Converts to integer index.
#'
#' @param rank_cols Integer/number, or logical, or names within \code{colnames(x_mat)}
#' @param x_mat the x matrix of interest
#' @export
rank_integer_index <- function(rank_cols, x_mat) {
  if (is.null(rank_cols)) {
    return(vector("integer", 0L))
  }

  if (all(rank_cols %in% 1L:ncol(x_mat))) {
    return(rank_cols)
  }

  if (is.logical(rank_cols)) {
    if (length(rank_cols) == ncol(x_mat)) {
      return(which(rank_cols))
    } else {
      stop("logical `rank_cols` must be same length as `ncol(x_mat)`")
    }
  } else {
    if (is.null(colnames(x_mat))) {
      stop("x_mat must have colnames to use named rank_cols")
    }

    if (any(!(rank_cols %in% colnames(x_mat)))) {
      stop("not all rank_cols are present in x_mat colnames")
    }

    return(which(colnames(x_mat) %in% rank_cols))
  }
}
rzgross/uRbanmatching documentation built on Dec. 22, 2021, 8:20 p.m.