Make an matrix from various input graph types.

to_matrix(x)

Arguments

x

graph object

Value

a matrix object

Details

Create the appropriate matrix from various inputs:

igraph

a bipartite network is turned into an incidence matrix, any other network becomes an adjacency matrix. If the original graph is weighted (ie. it has an edge attribute called 'weight'), the resulting matrix is weighted as well.

network

a bipartite network is turned into an incidence matrix, any other network becomes an adjacency matrix (non-sparse). If the original graph is weighted (ie. it has an edge attribute called 'weight'), the resulting matrix is weighted as well.

data.frame

the first two columns are taken as, respectively, denoting 'from' and 'to'. If it exists, the third column is used for the values of the cells in the matrix. If there are only two columns in the edgelist, the matrix is weighted in case an edge occurs multiple times. The graph is assumed to be directed, so one needs to symmetrize the matrix afterwards if the graph is undirected. In case the senders and receivers have no overlap in the edgelist, the graph is assumed to be bipartite and an incidence matrix (potentially weighted) is returned

Examples

actors <- data.frame(name=c("Alice", "Bob", "Cecil", "David", "Esmeralda"), 
   age=c(48,33,45,34,21),
   gender=c("F","M","F","M","F"))
relations <- data.frame(from=c("Bob", "Cecil", "Cecil", "David", 
   "David", "Esmeralda"),
   to = c("Alice", "Bob", "Alice", "Alice", "Bob", "Alice"), 
   same.dept = c(FALSE, FALSE, TRUE, FALSE, FALSE, TRUE), 
   friendship = c(4, 5, 5, 2, 1, 1), 
   advice = c(4, 5, 5, 4, 2, 3))