network
network copied to clipboard
Creating network objects from adjacency lists
PLEASE also add a NODELIST (ego, alter1, alter2). Empirical survey data mostly comes as a NODELIST. I have spent so much time with getting nodelists into statnet:
Originally posted by @jdohmen in https://github.com/statnet/network/issues/64#issuecomment-1156460908
A primitive tidyversian prototype seems straightforward:
txt <- "ego alter1 alter2 alter3
1 2 3 4
2 1 3
3 2 1
4 1"
d <- read.table(textConnection(txt), as.is = TRUE, fill = TRUE, header = TRUE)
d
#> ego alter1 alter2 alter3
#> 1 1 2 3 4
#> 2 2 1 3 NA
#> 3 3 2 1 NA
#> 4 4 1 NA NA
stopifnot({
library("dplyr")
requireNamespace("tidyr")
})
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
#> Loading required namespace: tidyr
d |>
tidyr::pivot_longer(- ego, values_to = "to") |>
filter(!is.na(to)) |>
select(from = ego, to, nomination = name)
#> # A tibble: 8 × 3
#> from to nomination
#> <int> <int> <chr>
#> 1 1 2 alter1
#> 2 1 3 alter2
#> 3 1 4 alter3
#> 4 2 1 alter1
#> 5 2 3 alter2
#> 6 3 2 alter1
#> 7 3 1 alter2
#> 8 4 1 alter1
... which gives an edgelist ready to be consumed by as.network()
data frame method.
The input in the example assumes that adjacency information is "reciprocated"/, i.e. if 1
and 2
are connected then 2
will appear adjacent in the row for 1
and vice versa. Thus each tie unnecessarily appears twice. I believe this corresponds to the adjacency list representation called-upon in some graph algorithms.
On the other hand, in case of "survey data" (presumably a name generator in an egocentric study) ties should be interpreted as directed (thus not reciprocated by design).