Conversion between adjacency matrix and edgelist

Description

Generates adjacency matrix from an edgelist and vice versa.

Usage

1
2
3
4
5
6
7
8
edgelist_to_adjmat(edgelist, w = NULL, t0 = NULL, t1 = NULL, t = NULL,
  simplify = TRUE, undirected = getOption("diffnet.undirected"),
  self = getOption("diffnet.self"),
  multiple = getOption("diffnet.multiple"), keep.isolates = TRUE,
  recode.ids = TRUE)

adjmat_to_edgelist(graph, undirected = getOption("diffnet.undirected", FALSE),
  keep.isolates = getOption("diffnet.keep.isolates", TRUE))

Arguments

edgelist

Two column matrix/data.frame in the form of ego -source- and alter -target- (see details).

w

Numeric vector. Strength of ties (optional).

t0

Integer vector. Starting time of the ties (optional).

t1

Integer vector. Finishing time of the ties (optional).

t

Integer scalar. Repeat the network t times (if no t0,t1 are provided).

simplify

Logical scalar. When TRUE and times=NULL it will return an adjacency matrix, otherwise an array of adjacency matrices. (see details).

undirected

Logical scalar. When TRUE only the lower triangle will be processed.

self

Logical scalar. When TRUE allows loops (self edges).

multiple

Logical scalar. When TRUE allows multiple edges.

keep.isolates

Logical scalar. When FALSE, rows with NA/NULL values (isolated vertices unless have autolink) will be droped (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).

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.

Edges with incomplete information (missing data on w or times) are not included on the graph. Incomplete cases are tagged using complete.cases and can be retrieved by the user by accessing the attribute incomplete.

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 either times or w, it will be removed. A full list of such edges can be retrieved from the attribute incomplete

  • Nodes and times ids coding

recode.ids=FALSE is useful when the vertices ids have already been coded. For example, after having use adjmat_to_edgelist, ids are correctly encoded, so when going back (using edgelist_to_adjmat) recode.ids should be FALSE.

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 with the following columns:

ego

Origin of the tie.

alter

Target of the tie.

value

Value in the adjacency matrix.

time

Either a 1 (if the network is static) or the time stamp of the tie.

Author(s)

George G. Vega Yon, Stephanie R. Dyal, Timothy B. Hayes & Thomas W. Valente

See Also

Other data management functions: as_diffnet, egonet_attrs, isolated, survey_to_diffnet

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Base data
set.seed(123)
n <- 5
edgelist <- rgraph_er(n, as.edgelist=TRUE)[,c("ego","alter")]
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 w
edgelist_to_adjmat(edgelist, w)
edgelist_to_adjmat(edgelist, w, undirected = TRUE)

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

# Using times and w
edgelist_to_adjmat(edgelist, t0 = times, w = w)
edgelist_to_adjmat(edgelist, t0 = times, undirected = TRUE, w = 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)

# Dynamic with spells -------------------------------------------------------
edgelist <- rbind(
   c(1,2,NA,1990),
   c(2,3,NA,1991),
   c(3,4,1991,1992),
   c(4,1,1992,1993),
   c(1,2,1993,1993)
)

graph <- edgelist_to_adjmat(edgelist[,1:2], t0=edgelist[,3], t1=edgelist[,4])

# Creating a diffnet object with it so we can apply the plot_diffnet function
diffnet <- as_diffnet(graph, toa=1:4)
plot_diffnet(diffnet, label=rownames(diffnet))

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.