R/dot-getVecNames.R

Defines functions .getVecNames

Documented in .getVecNames

#' @title Function for Generating Vector Names
#'
#' @description The function generates names for a vector that contains elements
#'   of the lower triangle of a matrix. The R code has been copied from
#'   \code{getNames} (not exported function of the \code{discordant} package)
#'   and changed such that the names are generated by columns (and not by rows
#'   as in the original function, which produces implausible results).
#'
#' @param x symmetric matrix for which the column and row names should be
#'   returned as a vector

.getVecNames <- function(x) {
  
  temp <- matrix(NA, nrow = nrow(x), ncol = nrow(x))
  
  diag <- lower.tri(temp, diag = FALSE)
  
  temp[diag] <- rep(1, sum(diag == TRUE))
  
  namesMatrix <- NULL
  
  for (i in 1:dim(temp)[2]) {
    outputCol <- temp[, i]
    index <- which(is.na(outputCol) == FALSE)
    
    if (length(index) > 0) {
      tempMatrix <- cbind(rownames(x)[index],
                          rep(rownames(x)[i], length(index)))
      namesMatrix <- rbind(namesMatrix, tempMatrix)
    }
  }
  
  vector_names <- apply(namesMatrix, 1, function(k) paste(k[1],
                                                          "_", k[2], sep = ""))
  return(vector_names)
}
stefpeschel/NetCoMi documentation built on Feb. 4, 2024, 8:20 a.m.