SAVE NETWORK DATA FROM THE FIRST ADD HEALTH COMMUNITY

Download R source file


              # Save network data from the first Add Health community

rm(list = ls())
setwd("C:/Users/fishe/Dropbox/SN&H Files/r_scripts")
library(statnet)
library(dplyr)
library(tidyr)
library(magrittr)

cmty1 <- read.csv("C:/Users/fishe/Dropbox/SN&H Files/SampleData/ahs_wpvar.csv") %>%
  filter(commcnt == 1)

el <- cmty1 %>%
  select(ego_nid, contains("fnid")) %>%
  gather(key = "drop.me", value = "alter", -ego_nid) %>%
  mutate(alter = ifelse(alter == 99999, NA, alter)) %>%
  select(-drop.me)

people <- unique(el$ego_nid)

add.health <- network.initialize(n = length(people))

el %<>% filter(!is.na(alter))
add.edges(add.health, tail = el$ego_nid, head = el$alter)

network.vertex.names(add.health) <- people

incl.vars <- c("race5", "grade", "sex", "POP_BEH", "IDG", "PSICK_SS", "PSICK_H",
               "PSICK_L", "IDG_SS", "POP_BEH_SS", "PSMOKES", "PHADSEX")

for (i in seq_along(incl.vars)) {
  set.vertex.attribute(add.health, attrname = incl.vars[i], 
                        value = cmty1[, incl.vars[i]])
}

add.health
save(add.health, file = "add_health_cmty1.Rdata")