Convert a network object into a numeric edgelist matrix
as.edgelist.RdConstructs an edgelist in a sorted format with defined attributes.
Usage
# S3 method for class 'network'
as.edgelist(
x,
attrname = NULL,
as.sna.edgelist = FALSE,
output = c("matrix", "tibble"),
...
)
# S3 method for class 'matrix'
as.edgelist(
x,
n,
directed = TRUE,
bipartite = FALSE,
loops = FALSE,
vnames = seq_len(n),
...
)
# S3 method for class 'tbl_df'
as.edgelist(
x,
n,
directed = TRUE,
bipartite = FALSE,
loops = FALSE,
vnames = seq_len(n),
...
)
is.edgelist(x)Arguments
- x
a
networkobject with additional class added indicating how it should be dispatched.- attrname
optionally, the name of an edge attribute to use for edge values; may be a vector of names if
output="tibble"- as.sna.edgelist
logical; should the edgelist be returned in edgelist form expected by the sna package? Ignored if
output="tibble"- output
return type: a
matrixor atibble; seeas.matrix.networkfor the difference.- ...
additional arguments to other methods
- n
integer number of vertices in network, value passed to the 'n' flag on edgelist returned
- directed
logical; is network directed, value passed to the 'directed' flag on edgelist returned
- bipartite
logical or integer; is network bipartite, value passed to the 'bipartite' flag on edgelist returned
- loops
logical; are self-loops allowed in network?, value passed to the 'loops' flag on edgelist returned
- vnames
vertex names (defaults to vertex ids) to be attached to edgelist for sna package compatibility
Value
A matrix in which the first two columns are integers giving the tail
(source) and head (target) vertex ids of each edge. The matrix will be given
the class edgelist.
The edgelist has additional attributes attached to it:
attr(,"n")the number of vertices in the original networkattr(,"vnames")the names of vertices in the original networkattr(,"directed")logical, was the original network directedattr(,"bipartite")was the original network bipartiteattr(,"loops")does the original network contain self-loops
Note that if the attrname attribute is used the resulting edgelist
matrix will have three columns. And if attrname refers to a
character attribute, the resulting edgelist matrix will be character rather
than numeric unless output="tibble".
Details
Constructs a edgelist matrix or tibble from a network, sorted tails-major
order, with tails first, and, for undirected networks, tail < head. This
format is required by some reverse-depending packages (e.g. ergm)
The as.matrix.network.edgelist provides similar functionality
but it does not enforce ordering or set the edgelist class and so
should be slightly faster.
is.edgelist tests if an object has the class 'edgelist'
See also
See also as.matrix.network.edgelist
Examples
data(emon)
as.edgelist(emon[[1]])
#> [,1] [,2]
#> [1,] 1 2
#> [2,] 1 3
#> [3,] 1 4
#> [4,] 1 5
#> [5,] 1 7
#> [6,] 1 8
#> [7,] 1 9
#> [8,] 1 10
#> [9,] 1 11
#> [10,] 1 14
#> [11,] 2 1
#> [12,] 2 3
#> [13,] 2 7
#> [14,] 2 8
#> [15,] 2 9
#> [16,] 2 10
#> [17,] 2 14
#> [18,] 3 1
#> [19,] 3 2
#> [20,] 3 4
#> [21,] 3 5
#> [22,] 3 6
#> [23,] 3 7
#> [24,] 3 8
#> [25,] 3 9
#> [26,] 3 10
#> [27,] 3 11
#> [28,] 3 12
#> [29,] 3 13
#> [30,] 4 2
#> [31,] 4 3
#> [32,] 4 7
#> [33,] 4 9
#> [34,] 4 10
#> [35,] 5 7
#> [36,] 5 8
#> [37,] 5 13
#> [38,] 7 3
#> [39,] 7 8
#> [40,] 7 10
#> [41,] 7 11
#> [42,] 7 12
#> [43,] 7 13
#> [44,] 8 1
#> [45,] 8 2
#> [46,] 8 4
#> [47,] 8 5
#> [48,] 8 6
#> [49,] 8 7
#> [50,] 8 9
#> [51,] 8 10
#> [52,] 8 11
#> [53,] 8 12
#> [54,] 8 13
#> [55,] 8 14
#> [56,] 9 1
#> [57,] 9 6
#> [58,] 9 7
#> [59,] 9 8
#> [60,] 9 10
#> [61,] 9 13
#> [62,] 9 14
#> [63,] 10 7
#> [64,] 10 9
#> [65,] 11 7
#> [66,] 11 9
#> [67,] 11 10
#> [68,] 11 13
#> [69,] 11 14
#> [70,] 12 3
#> [71,] 12 7
#> [72,] 12 8
#> [73,] 12 9
#> [74,] 13 3
#> [75,] 13 7
#> [76,] 13 8
#> [77,] 13 9
#> [78,] 13 10
#> [79,] 13 11
#> [80,] 13 12
#> [81,] 14 1
#> [82,] 14 5
#> [83,] 14 8
#> attr(,"n")
#> [1] 14
#> attr(,"vnames")
#> [1] "Wyoming.Disaster.and.Civil.Defense.Agnecy"
#> [2] "Wyoming.State.National.Guard..Army.and.Air."
#> [3] "Wyoming.State.Highway.Patrol"
#> [4] "Francis.E..Warren.Air.Force.Base..fire.unit."
#> [5] "Red.Cross"
#> [6] "Salvation.Army"
#> [7] "Laramie.County.Sheriff.s.Office"
#> [8] "Laramie.County...Cheyenne.Civil.Defense.Agency"
#> [9] "Cheyenne.Fire.Department"
#> [10] "Cheyenne.Police.Department"
#> [11] "Laramie.County.Fire.District..1..volunteer."
#> [12] "Laramie.County.Fire.District..2..volunteer."
#> [13] "A.1.Ambulance.Service"
#> [14] "Shy.Wy.HAM.Radio.Club"
#> attr(,"directed")
#> [1] TRUE
#> attr(,"bipartite")
#> [1] FALSE
#> attr(,"loops")
#> [1] FALSE
#> attr(,"class")
#> [1] "matrix_edgelist" "edgelist" "matrix" "array"
as.edgelist(emon[[1]],output="tibble")
#> # A tibble: 83 × 2
#> .tail .head
#> <int> <int>
#> 1 1 2
#> 2 1 3
#> 3 1 4
#> 4 1 5
#> 5 1 7
#> 6 1 8
#> 7 1 9
#> 8 1 10
#> 9 1 11
#> 10 1 14
#> # ℹ 73 more rows
# contrast with unsorted columns of
as.matrix.network.edgelist(emon[[1]])
#> [,1] [,2]
#> [1,] 2 1
#> [2,] 3 1
#> [3,] 8 1
#> [4,] 9 1
#> [5,] 14 1
#> [6,] 1 2
#> [7,] 3 2
#> [8,] 4 2
#> [9,] 8 2
#> [10,] 1 3
#> [11,] 2 3
#> [12,] 4 3
#> [13,] 7 3
#> [14,] 12 3
#> [15,] 13 3
#> [16,] 1 4
#> [17,] 3 4
#> [18,] 8 4
#> [19,] 1 5
#> [20,] 3 5
#> [21,] 8 5
#> [22,] 14 5
#> [23,] 3 6
#> [24,] 8 6
#> [25,] 9 6
#> [26,] 1 7
#> [27,] 2 7
#> [28,] 3 7
#> [29,] 4 7
#> [30,] 5 7
#> [31,] 8 7
#> [32,] 9 7
#> [33,] 10 7
#> [34,] 11 7
#> [35,] 12 7
#> [36,] 13 7
#> [37,] 1 8
#> [38,] 2 8
#> [39,] 3 8
#> [40,] 5 8
#> [41,] 7 8
#> [42,] 9 8
#> [43,] 12 8
#> [44,] 13 8
#> [45,] 14 8
#> [46,] 1 9
#> [47,] 2 9
#> [48,] 3 9
#> [49,] 4 9
#> [50,] 8 9
#> [51,] 10 9
#> [52,] 11 9
#> [53,] 12 9
#> [54,] 13 9
#> [55,] 1 10
#> [56,] 2 10
#> [57,] 3 10
#> [58,] 4 10
#> [59,] 7 10
#> [60,] 8 10
#> [61,] 9 10
#> [62,] 11 10
#> [63,] 13 10
#> [64,] 1 11
#> [65,] 3 11
#> [66,] 7 11
#> [67,] 8 11
#> [68,] 13 11
#> [69,] 3 12
#> [70,] 7 12
#> [71,] 8 12
#> [72,] 13 12
#> [73,] 3 13
#> [74,] 5 13
#> [75,] 7 13
#> [76,] 8 13
#> [77,] 9 13
#> [78,] 11 13
#> [79,] 1 14
#> [80,] 2 14
#> [81,] 8 14
#> [82,] 9 14
#> [83,] 11 14
#> attr(,"n")
#> [1] 14
#> attr(,"vnames")
#> [1] "Wyoming.Disaster.and.Civil.Defense.Agnecy"
#> [2] "Wyoming.State.National.Guard..Army.and.Air."
#> [3] "Wyoming.State.Highway.Patrol"
#> [4] "Francis.E..Warren.Air.Force.Base..fire.unit."
#> [5] "Red.Cross"
#> [6] "Salvation.Army"
#> [7] "Laramie.County.Sheriff.s.Office"
#> [8] "Laramie.County...Cheyenne.Civil.Defense.Agency"
#> [9] "Cheyenne.Fire.Department"
#> [10] "Cheyenne.Police.Department"
#> [11] "Laramie.County.Fire.District..1..volunteer."
#> [12] "Laramie.County.Fire.District..2..volunteer."
#> [13] "A.1.Ambulance.Service"
#> [14] "Shy.Wy.HAM.Radio.Club"