R/compare_rows.R

Defines functions compare_rows

Documented in compare_rows

# compare_rows
#' Compare rows in a matrix
#'
#' For all pairs of rows in a matrix, calculate the proportion of mismatches or the RMS difference.
#'
#' @param mat Numeric matrix. Should be integers in the case `method="prop_mismatches"`.
#' @param method Indicates whether to use proportion mismatches or the
#' RMS difference. Missing values are omitted.
#'
#' @useDynLib broman, .registration=TRUE
#' @export
#' @return A square matrix of dimension `nrow(mat)` with
#' `NA`s on the diagonal and the calculated statistic in the
#' body.
#'
#' @examples
#' n <- 10
#' p <- 200
#' x <- matrix(sample(1:4, n*p, replace=TRUE), ncol=p)
#' d <- compare_rows(x)

compare_rows <-
    function(mat, method=c("prop_mismatches", "rms_difference"))
{

    method <- match.arg(method)

    if(!is.matrix(mat))
        stop("mat should be a matrix")

    n <- nrow(mat)
    p <- ncol(mat)

    if(method=="prop_mismatches") {
        z <- .C("R_compare_rows_mismatch",
                as.integer(mat),
                as.integer(n),
                as.integer(p),
                d=as.double(rep(0, n*n)),
                NAOK=TRUE,
                PACKAGE="broman")
    } else {
        z <- .C("R_compare_rows_rmsd",
                as.double(mat),
                as.integer(n),
                as.integer(p),
                d=as.double(rep(0, n*n)),
                NAOK=TRUE,
                PACKAGE="broman")
    }

    d <- matrix(z$d, nrow=n, ncol=n)
    diag(d) <- NA
    dimnames(d) <- list(rownames(mat), rownames(mat))

    d
}

Try the broman package in your browser

Any scripts or data that you put into this service are public.

broman documentation built on July 8, 2022, 5:07 p.m.