Learn R Programming

netdiffuseR (version 1.16.2)

edgelist_to_adjmat: Conversion between adjacency matrix and edgelist

Description

Generates adjacency matrix from an edgelist and vice versa.

Usage

edgelist_to_adjmat(edgelist, weights = NULL, times = NULL, t = NULL,
  simplify = TRUE, undirected = getOption("diffnet.undirected"),
  self = getOption("diffnet.self"),
  multiple = getOption("diffnet.multiple"), use.incomplete = TRUE,
  recode.ids = TRUE)

adjmat_to_edgelist(graph, undirected = getOption("diffnet.undirected"))

Arguments

edgelist
Two column matrix/data.frame in the form of ego -source- and alter -target- (see details).
weights
Numeric vector. Strength of ties (optional).
times
Integer vector. Starting time of the ties (optional).
t
Integer scalar. If times but want to repeat the network t times.
simplify
Logical scalar. When TRUE and times=NULL it will return an adjacency matrix, otherwise an array of adjacency matrices.
undirected
Logical scalar. TRUE when the graph is undirected.
self
Logical scalar. TRUE when self edges are excluded.
multiple
Logical scalar. TRUE when multiple edges should not be included (see details).
use.incomplete
Logical scalar. When FALSE, rows with NA/NULL values (isolated vertices unless have autolink) will be droped and will not be considered in the graph, which may reduce the size of the adjacency matrix (see details).
recode.ids
Logical scalar. When TRUE ids are recoded using as.factor (see details).
graph
Any class of accepted graph format (see netdiffuseR-graphs).

Value

  • In the case of edgelist_to_adjmat either an adjacency matrix (if times is NULL) or an array of these (if times is not null). For adjmat_to_edgelist the output is an edgelist.

code

recode.ids

Details

When converting from edglist to adjmat the function will recode the edgelist before starting. The user can keep track after the recording by checking the resulting adjacency matrices' row.names. In the case that the user decides skipping the recoding (because wants to keep vertices index numbers, implying that the resulting graph will have isolated vertices), he can override this by setting recode.ids=FALSE (see example).

When multiple edges are included, multiple=TRUE,each vertex between ${i,j}$ will be counted as many times it appears in the edgelist. So if a vertex ${i,j}$ appears 2 times, the adjacency matrix element (i,j) will be 2.

Including incomplete cases, use.incomplete=TRUE, can lead to an adjacency matrix with isolated vertices. Otherwise, when use.incomplete=FALSE, if all the edges in which a vertex participates have incomplete information in any of the variables (a NA, NULL or NaN value, see complete.cases), it will be dropped from the graph, thus, reducing the size of the adjacency matrix by not including isolated vertices. Notice that the best way of addingisolated vertices is to include them in the edgelist as connecting to themselves. The option self=FALSE will not drop the isolated vertices (but the adjacency matrix will have a 0-diagonal) but the algorithm will include them on the graph.

The function performs several checks before starting to create the adjacency matrix. These are:

  • Dimensions of the inputs, such as number of columns and length of vectors
Having complete cases. If anly edge has a non-numeric value such as NAs or NULL in any variable (including times and weights), it will be removed. A full list of such edges can be retrieved from the attribute incomplete Nodes and times ids coding

See Also

Other data management functions: isolated

Examples

Run this code
# Base data
set.seed(123)
n <- 5
edgelist <- rgraph_er(n, as.edgelist=TRUE)
times <- sample.int(3, nrow(edgelist), replace=TRUE)
w <- abs(rnorm(nrow(edgelist)))

# Simple example
edgelist_to_adjmat(edgelist)
edgelist_to_adjmat(edgelist, undirected = TRUE)

# Using weights
edgelist_to_adjmat(edgelist, w)
edgelist_to_adjmat(edgelist, w, undirected = TRUE)

# Using times
edgelist_to_adjmat(edgelist, times = times)
edgelist_to_adjmat(edgelist, times = times, undirected = TRUE)

# Using times and weights
edgelist_to_adjmat(edgelist, times = times, weights = w)
edgelist_to_adjmat(edgelist, times = times, undirected = TRUE, weights = w)

# Not recoding ----------------------------------------------------
# Notice that vertices 3, 4 and 5 are not present in this graph.
graph <- matrix(c(
 1,2,6,
 6,6,7
), ncol=2)

# Generates an adjmat of size 4 x 4
edgelist_to_adjmat(graph)

# Generates an adjmat of size 7 x 7
edgelist_to_adjmat(graph, recode.ids=FALSE)

Run the code above in your browser using DataLab