R/mixingmatrix.R

Defines functions mixingmatrix.networkLite

Documented in mixingmatrix.networkLite

#' @rdname mixingmatrix
#'
#' @title Extract Mixing Matrix from `networkLite`
#'
#' @param object A `networkLite` object.
#' @param attr The name of a vertex attribute in `object`.
#' @param ... additional arguments
#'
#' @return
#' The mixing matrix (of class `table`) for `object` and `attr`.
#'
#' @export
#'
mixingmatrix.networkLite <- function(object, attr, ...) {
  nw <- object

  all_attr <- get.vertex.attribute(nw, attr)

  if (is.bipartite(nw)) {
    row_levels <- sort(unique(all_attr[seq_len(nw %n% "bipartite")]))
    col_levels <- sort(unique(all_attr[-seq_len(nw %n% "bipartite")]))
  } else {
    row_levels <- sort(unique(all_attr))
    col_levels <- row_levels
  }

  el <- as.edgelist(nw)

  m <- table(from = factor(all_attr[el[, 1]], levels = row_levels),
             to = factor(all_attr[el[, 2]], levels = col_levels))

  if (!is.bipartite(nw) && !is.directed(nw)) {
    m <- m + t(m) - diag(diag(m))
  }

  m
}

Try the networkLite package in your browser

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

networkLite documentation built on March 31, 2023, 9:58 p.m.