Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating network objects from adjacency lists #79

Open
mbojan opened this issue Jun 15, 2022 · 1 comment
Open

Creating network objects from adjacency lists #79

mbojan opened this issue Jun 15, 2022 · 1 comment

Comments

@mbojan
Copy link
Member

mbojan commented Jun 15, 2022

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:

NODELIST

Originally posted by @jdohmen in #64 (comment)

@mbojan
Copy link
Member Author

mbojan commented Jun 15, 2022

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant