library(igraph) setwd("~/Documents/Violence Cascades/Raw Data/") load('lcc.RData') library(foreach) library(doMC) registerDoMC(cores=4) lcc2 = lcc#remove.edge.attribute(lcc,'weight') vics = split(vic_ids, ceiling(seq_along(vic_ids)/500)) dag_dat_lcc = c() for(i in 1:length(vics)){ ptm = proc.time() print(c(i,length(vics))) vic_ids = unlist(vics[i], use.names=F) ddl = foreach (u = vic_ids, .combine=rbind) %dopar% { if ((which(vic_ids==u) %% 100)==0) print(which(vic_ids==u)) nbhd = unlist(neighborhood(lcc2,nodes=u,order=3)) # get nodes within neighborhood nbhd = nbhd[-1] # don't want to include u in the neighborhood dists = as.numeric(shortest.paths(lcc2,u,nbhd)) # make edge for every infection ddlu = data.frame(matrix(nrow=1,ncol=4)) ei = 1 for (j in c(17:21,16)){ tu = lcc_verts[u,j] if (is.na(tu)) next ddlu[ei:(ei+length(nbhd)-1),] = data.frame(rep(u,length(nbhd)), nbhd, rep(tu,length(nbhd)), dists, row.names=NULL) ei = ei + length(nbhd) } return(ddlu) } dag_dat_lcc = rbind(dag_dat_lcc,ddl) print(proc.time()-ptm) } colnames(dag_dat_lcc) = c('from','to','t1','dist') rownames(dag_dat_lcc) = NULL save(dag_dat_lcc, file='dag_dat_lcc.RData') write.csv(dag_dat_lcc, file='dag_dat_lcc.csv') # dag_dat_vics = dag_dat_lcc[!is.na(dag_dat_lcc$t2),] # save(dag_dat_lcc_vics, file='Results/dag_dat_lcc_vics.RData') # write.csv(dag_dat_lcc_vics, file='Results/dag_dat_lcc_vics.csv') #### # create lcc_vic_times vic_times_lcc = lcc_verts[,c('name','nonfatal_day_1','nonfatal_day_2', 'nonfatal_day_3','nonfatal_day_4', 'nonfatal_day_5','fatal_day')] save(vic_times_lcc, file='vic_times_lcc.RData') write.csv(vic_times_lcc, file='vic_times_lcc.csv')