R/fill_NA.R

Defines functions fill_na

Documented in fill_na

#' Fill NAs in an LD matrix
#'
#' Trickier than it looks.
#'
#' \code{
#' BST1_LD_matrix <- echodata::BST1_LD_matrix
#' LD_matrix <- fill_na(BST1_LD_matrix)
#' }
#'
#' @param LD_matrix LD matrix
#' @param fillNA Fill all NAs in the \code{LD_matrix} with \code{fillNA} value.
#' @param verbose Print messages.
#'
#' @keywords internal
fill_na <- function(LD_matrix,
                    fillNA = 0,
                    verbose = FALSE) {
    messager("+ echoLD:: Removing unnamed rows/cols", v = verbose)
    # First, filter any rows/cols without names
    LD_matrix <- data.frame(as.matrix(LD_matrix))
    LD_matrix <- LD_matrix[
        rownames(LD_matrix) != ".",
        colnames(LD_matrix) != "."
    ]
    LD_matrix_orig <- LD_matrix

    if (!is.null(fillNA)) {
        messager("+ echoLD:: Replacing NAs with", fillNA, v = verbose)
        if (sum(is.na(LD_matrix)) > 0) {
            LD_matrix[is.na(LD_matrix)] <- 0
        }
    }
    # Check for duplicate SNPs
    LD_matrix <- LD_matrix[
        rownames(LD_matrix)[!base::duplicated(rownames(LD_matrix))],
        colnames(LD_matrix)[!base::duplicated(colnames(LD_matrix))]
    ]
    return(LD_matrix)
}
RajLabMSSM/echotabix documentation built on Nov. 21, 2023, 8:02 a.m.