# read in the edge list from our githubel <-read.table("https://raw.githubusercontent.com/mahoffman/stanford_networks/main/data/comm59.dat.txt", header = T)# Read in attributes from our githubattributes <-read.table("https://raw.githubusercontent.com/mahoffman/stanford_networks/main/data/comm59_att.dat.txt", header = T)# add an ID columnattributes$ID <-1:nrow(attributes)
# Indexing data so that you only put in certain columnsel_no_weight <- el[,1:2] # We will ignore the ranking variable for now.colnames(el_no_weight) <-c("from", "to")el_no_weight <-as.matrix(el_no_weight) # igraph requires a matrix# convert ids to characters so they are preserved as namesel_no_weight <-apply(el_no_weight,2,as.character)# Graph the networknet59 <-graph.edgelist(el_no_weight, directed = T)# Finally, add attributes # First link vertex names to their place in the attribute datasetlinked_ids <-match(V(net59)$name, attributes$ID)# Then we can use that to assign a variable to each user in the networkV(net59)$race <- attributes$race[linked_ids]V(net59)$sex <- attributes$sex[linked_ids]V(net59)$grade <- attributes$grade[linked_ids]V(net59)$school <- attributes$school[linked_ids]net59