9.1 Load data
Regenerate data from previous chapter. (code below copied from previous chapter)
#read data
<-"https://raw.githubusercontent.com/mahoffman/stanford_networks/main/data/gss_local_nets.csv"
gss_url <- read_csv(gss_url) gss
## Rows: 1426 Columns: 41
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (13): sex, race, partyid, relig, educ2, educ3, educ4, educ5, relig1, rel...
## dbl (28): age, numgiven, close12, close13, close14, close15, close23, close2...
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
#subset ties data
<- gss %>%
ties ::select(starts_with("close"))
dplyr
#set zero links as missing links
==0] <- NA
ties[ties#repondents where all links among others are missing
<- rowSums(is.na(ties))==ncol(ties)
others_missing #remove any respondent that falls in any of the above
<- ties[!(others_missing),]
ties
#make a function to generate a network of ties
<- function(tie){
make_ego_nets_simple #get the all possible links among others
<- tie %>% unlist
tie #remove missing links
<- tie[!is.na(tie)]
tie #remove zero links
<- tie[tie!=0]
tie #get the identity of linked pairs
<- str_extract(names(tie), "[0-9]+")
others #split the linked others
<- str_split(others, "",simplify = TRUE) %>% as.data.frame
others_link #make edge list of others
<- cbind(others_link, tie)
others_link #ego graph with
graph_from_data_frame(others_link,
directed=FALSE)
}#get all graphs
<- apply(ties,1,make_ego_nets_simple)
ego_nets_simple #
1,] ties[
## # A tibble: 1 × 10
## close12 close13 close14 close15 close23 close24 close25 close34 close35
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1 2 NA NA 2 2 NA 1 NA
## # ℹ 1 more variable: close45 <dbl>
plot(ego_nets_simple[[1]])