R/list-to-matrix.R

Defines functions list_to_matrix

Documented in list_to_matrix

#' Convert a list of vectors to a binary matrix
#'
#' Convert a list of sets into a binary matrix.
#'
#' @param sets named list of sets (vectors) to be converted to binary matrix.
#' @return binary matrix with a column for each set. rownames of the matrix represent the union of all sets.
#' A '1' indicates the inclusion of the element in the set.
#' @export
#' @examples
#' sets <- list(
#'   "set1" = letters[1:5],
#'   "set2" = letters[2:6],
#'   "set3" = letters[1:7]
#' )
#'
#' list_to_matrix(sets)
list_to_matrix <- function(sets) {
  stopifnot("List of vectors must be supplied" = is(sets, "list"))
  union_all <- Reduce(union, sets)

  if (anyNA(union_all)) {
    message("NA values present in union of all sets. NA values will be dropped in final matrix")
    union_all <- union_all[!is.na(union_all)]
  }

  mat <- matrix(
    data = 0L,
    nrow = length(union_all),
    ncol = length(sets),
    dimnames = list(union_all, names(sets))
  )

  for (i in seq_along(sets)) {
    mat[unique(sets[[i]][!is.na(sets[[i]])]), i] <- 1L
  }
  mat
}
coriell-research/coriell documentation built on March 29, 2025, 2:19 p.m.