#install.packages('countrycode') library(countrycode) # The Correlates of War (COW) uses a particular set of country codes that is different # from the ISO3 codes supported by various graphing and charting libraries. # This function uses the R countrycode package for the necessary conversion from COW to ISO3. # However, the package does not have any data for Kosovo which is present in the COW Contiguity # dataset and so this is handled separately by converting it to Serbia # Refer https://en.wikipedia.org/wiki/Kosovo%E2%80%93Serbia_relations lookupISO3byCOWC <- function(cowcVal) { if (cowcVal == c('KOS')) { iso3cVal <- c('SRB') #iso3cVal <- c('KSV') } else { iso3cVal <- countrycode(cowcVal, 'cowc', 'iso3c') } return(iso3cVal) } # Uses the countrycode_data dataframe from the countrycode package to dump the list # of ISO3 codes for all countries. Again Kosovo is added separately. Refer to the # description of the above function lookupISO3byCOWC generateListOfAllCountriesISO3 <- function(outFilename) { df_countries <- countrycode_data[, c('cowc','iso3c', 'country.name.en')] #kos_add <-data.frame("KOS","KSV", "Kosovo") #names(kos_add)<-c('cowc','iso3c', 'country.name.en') #df_countries <- rbind(df_countries, kos_add) all_countries_iso3 <- subset(df_countries, select=c('iso3c'), !is.na(iso3c)) N <- nrow(all_countries_iso3) cat("Number of countries/nodes (N) = ", N, '\n') write.table(all_countries_iso3,file=outFilename, quote=F, row.names=F, col.names=FALSE) cat("Generated output data file containing list of all countries (ISO3) :", outFilename, '\n') } # Processes the Correlates of War Contiguity dataset and prints of pairs of countries # (ISO3 codes) that share a land or river border as on the latest timeframe 2001612. Refer to the data # dictionary for details of contiguity types generateNeighborPairs <- function(inDataFilename, outFilename) { df <- read.csv(inDataFilename, stringsAsFactors=FALSE) df_edg <- subset(df, conttype==1 & end==201612, select=c('statelab','statehab')) states <- unique(c(df_edg$statelab,df_edg$statehab)) Nbar <-length(states) M <- nrow(df_edg) cat("Number of countries with land or river border(s) (Nbar) =", Nbar, "; Number of borders/edges M =", M, '\n') df_edg$statelab <- sapply(df_edg$statelab, lookupISO3byCOWC ) df_edg$statehab <- sapply(df_edg$statehab, lookupISO3byCOWC) write.table(df_edg,file=outFilename, quote=F,sep=" ",row.names=FALSE, col.names=FALSE) cat("Generated output data file containing neighbor country pairs (ISO3) :", outFilename,'\n') } ##### Main ##### generateListOfAllCountriesISO3("data/all-countries-iso3.dat") generateNeighborPairs("data/contdir.csv", "data/country-borders-landorriver-iso3.dat")