#Load necessary libraries library(igraph) library(tidyverse) #Load in the boater matrix we're using. boats = read.csv("Kao's boater networks/boats1.csv") #Lake info spreadsheet--contains info on infestation statuses of all lakes, their county affiliation, their dow numbers and names, etc. AIS = read.csv("lake_info.csv") AIS$DOW = as.numeric(AIS$DOW) #Warning can be ignored here. #Filter out and rename columns AIS = dplyr::select(AIS, DOW, zm=zm2019, ss=ss2019, ew=ew2019, sf=sf2019) #Join the two files together so we have infestation statuses by edge. boats2 = left_join(boats, AIS, by = c("dow_origin" = "DOW"), relationship = "many-to-many") boats3 = left_join(boats2, AIS, by = c("dow_destination" = "DOW"), suffix = c(".donor", ".receiver"), relationship = "many-to-many") #Remove self edges and duplicated edges from the joins, then calculate b.ij indicator variable with and without SWF included for each edge. Bij is > 0 for every 1 species that could move along that edge that the originating lake has and the receiving lake does not. boats4 = boats3 %>% filter(dow_origin != dow_destination) %>% distinct(dow_origin, dow_destination, .keep_all = TRUE) %>% rowwise() %>% mutate( bij.swf = sum( c(zm.donor, ss.donor, ew.donor, sf.donor) * (1-c(zm.receiver, ss.receiver, ew.receiver, sf.receiver))), bij.noswf = sum( c(zm.donor, ss.donor, ew.donor) * (1-c(zm.receiver, ss.receiver, ew.receiver))) ) #Select down to just the essential columns and filter out all edges not carrying risky boats. boats_reduce = boats4 %>% dplyr::select(from = dow_origin, to = dow_destination, weight, bij = bij.swf) %>% filter(bij > 0) boats_reduce_noswf = boats4 %>% dplyr::select(from = dow_origin, to = dow_destination, weight, bij = bij.noswf) %>% filter(bij > 0) #Save the resulting files. saveRDS(boats_reduce, "boats_reduce") saveRDS(boats_reduce_noswf, "boats_reduce_noswf")