R/to_adjacency_matrix.R

Defines functions to_adjacency_matrix

Documented in to_adjacency_matrix

#' Convert a data frame to an adjacency matrix
#' 
#' Convert a data frame of pairwise interactions into an adjacency matrix
#' between all objects. 
#' 
#' @param dat a data frame with the objects that are interacting in the 
#' first two columns
#' 
#' @return a square adjacency matrix with all unique values of the first 
#' two columns of \code{dat} as row and column names 
#' 
#' @export
to_adjacency_matrix <- function(dat) {
  nodes <- unique(c(dat[[1]], dat[[2]]))
  adj <- matrix(0, nrow = length(nodes), ncol = length(nodes),
                   dimnames = list(nodes, nodes))
  adj[cbind(dat[[1]], dat[[2]])] <- 1
  adj[cbind(dat[[2]], dat[[1]])] <- 1
  return(adj)
}
skinnider/flavin documentation built on May 6, 2019, 10:49 a.m.