nwfromedge
Import network data from an edge list
Syntax
nwfromedge
fromid
toid
[tievalue]
[if]
[,
name(newnetname)
xvars
labs(lab1 lab2 ...)
undirected
directed
twomode
valued
unvalued
keeporiginal
selfloop]
name(newnetname) |
name of the new network; default = the name of tievalue if given, otherwise network |
xvars |
generate Stata variables for the network |
labs(lab1 lab2 …) |
overwrite node labels; separate the labels by blanks or by commas (lab1,lab2,…); use commas when a label contains a blank |
undirected |
force the network to be undirected (alias: forceundirected) |
directed |
force the network to be directed (alias: forcedirected) |
twomode |
declare a two-mode (bipartite) network instead - fromid/toid are the mode-1/mode-2 id variables, not a directed ego/alter pair. An exact alias for nw2fromedge, forwarding name()/xvars only; cannot be combined with directed/undirected/forcedirected/forceundirected. See nw2fromedge for the full two-mode-specific behavior (same-label disambiguation, mode assignment, project()) |
valued |
mark the network as valued (default if a value variable is given, also when every value is 1) |
unvalued |
mark the network as binary even if a value variable is given |
noclear |
do not clear existing dataset: the data are kept as they were, and the rows of the new network’s nodes are added |
selfloop |
keep the ties of a node with itself (rows with fromid = toid); by default they are dropped |
keeporiginal |
generate variable _nodeoriginal with the original identifier of each node (from fromid/toid) on the node’s row |
replace |
if a network named newnetname already exists, drop it and use this name anyway (see nwset for the same convention) |
labprefix(string) |
prefix used for auto-generated node labels when fromid / toid are numeric and labs() is not specified; default = n - named labprefix(), not prefix(), to avoid colliding with nwrecode’s unrelated prefix() (which prefixes new network names, not node labels) |
overwrite |
forwarded to nwload governing whether this command’s own generated Stata variables overwrite existing ones of the same name - unrelated to replace above, which is about the network, not Stata variables |
Description
nwfromedge imports a network from a dataset in edgelist format.
An edgelist or arclist is a set of two (or three in the case of a valued network) variables representing relations. Nodes are identified by entries in the cells. For example, the data
+---------------+
| fromid toid |
|---------------|
1. | 1 2 |
2. | 2 3 |
3. | 4 2 |
+---------------+
stores information about three ties (1=>2), (2=>3) and (4=>2) among four unique network nodes. The variables defining the edges can also be string variables.
+--------------------------+
| fromid toid value|
|--------------------------|
1. | Peter Thomas 1 |
2. | Tim Peter 3 |
3. | Mathilde Thomas 2 |
+--------------------------+
Here, there are also three relationships: (Peter => Thomas), (Tim => Peter) and (Mathilde => Thomas).
If a pair (fromid, toid) is listed in more than one row, the last of these rows gives the tie value, for numeric and string identifiers alike.
The following command declares such data as network data:
. nwfromedge fromid toid value, name(mynet)
To keep a node without ties (an isolate), give it a row of its own with the other identifier missing ("" for string identifiers, . for numeric ones): the node is created, the row adds no tie. A row with both identifiers missing is ignored. For example, the rows Anna Sophie and Fion "" make a network of three nodes with one tie, Fion an isolate.
This automatically generates the relevant meta-information for the network and makes it available for other programs under the netname mynet. In case no name() is specified, the command tries to come up with a suitable name for the new network. It tries the name of the variable tievalue when one is given and network otherwise; however, if a network with this name already exists, it comes up with an alternative name network_1 and so on (see nwvalidate).
After a network has been declared, one can refer to it by its netname, just as if one would refer to a varname. For example, this makes a network plot of mynet.
. nwplot mynet
Or alternatively, this calculates the betweenness centrality of the nodes in mynet.
. nwbetween mynet
By default, nwfromedge recognizes if a network is directed or undirected, i.e. for each dyad entry (i,j) there is also a dyad entry (j,i). However, this automatic detection can be overwritten with the options undirected and directed. With undirected, a tie that is listed in only one direction is stored as it is (a negative value stays negative); if both directions are listed with different values, the larger value is kept.
One can also transfrom any network that exists in memory into such an edgelist with nwtoedge.
Network types
Directed: options directed, undirected, forcedirected and forceundirected. Valued: a third column supplies the tie values. Two-mode: yes - with twomode, an alias for nw2fromedge. Signed: with undirected a tie listed once keeps its sign. See nwnetworktypes.
Examples
This loads a network dataset from the internet and transforms the network glasgow1 into an edgelist.
. nwwebuse glasgow, nwclear
. nwtoedge glasgow1
nwtoedge produces variables _ego/_alter (not _fromid/_toid) plus a tie-value column named after the network itself (glasgow1 here, not _link) - and, being every possible pair rather than a compact edgelist, it needs filtering down to the real ties before being turned back into a network:
. keep if glasgow1 == 1
. nwfromedge _ego _alter, name(mynet)
glasgow1 has 50 nodes but only 47 with at least one tie; mynet still comes out with all 50 - the 3 isolates nwtoedge could not write as edgelist rows are added back automatically, with no extra step needed. This only works because nwtoedge itself produced this particular edgelist; in an edgelist typed in by hand or imported from an external source, give each isolate a row with a missing other identifier.
Also see
nwcommands version 3.25.0, 14 November 2026 (check your installed version)