#Ed Lefley
#2021 11 09
#Joining all addresses tables
#based on https://medium.com/coinmonks/merging-multiple-dataframes-in-r-72629c4632a3

#install.packages('dplyr')
library('dplyr')

#install.packages('tibble')
library('tibble')

library('stringr')

#Set Colnames to be same for bind for short Geocode
colnames(CSC_C_Ag7_NN_U_A, do.NULL = FALSE)
colnames(CSC_C_Ag7_NN_U_A) <- c("Ctry")

Short_Geocode_Ag7 <- rbind(Country_List_C_Ag7_U, CSC_C_Ag7_NN_U_A)

#WoS_Ag7

Combined_Addresses_Ag7 <- do.call("cbind", 
  list(WoS_Ag7U,
       WoS_Ag7U_REV_RJ,
       Country_List_C_Ag7_NN,
       CSC_C_Ag7_NN))

Combined_Addresses_Ag7$Australian <- (1*(str_detect(Combined_Addresses_Ag7$Ctry, "Australia", negate = FALSE)))


#NROW(Combined_Addresses)


colnames(Combined_Addresses_Ag7, do.NULL = FALSE)
colnames(Combined_Addresses_Ag7) <- c("Full_Address","Short_Address","Country","Country_City","Australian")

#View(Combined_Addresses_Ag7)

#Use this for the next bit? https://gist.github.com/dfalster/5589956


Geocode_this_Ag7 <- ifelse(Combined_Addresses_Ag7$Australian, Combined_Addresses_Ag7$Country_City, Combined_Addresses_Ag7$Country)
Geocode_this_Ag7 <- as.data.frame(Geocode_this_Ag7)
#View(Geocode_this_Ag7)

Geocoded_Addresses_Ag7 <- do.call("cbind",
                             list(Combined_Addresses_Ag7,
                                  Geocode_this_Ag7))
colnames(Geocoded_Addresses_Ag7, do.NULL = FALSE)
colnames(Geocoded_Addresses_Ag7) <- c("Full Address","Short_Address", "Country","Geocoding_Address","Australian","Geocode_this")

#View(Geocoded_Addresses_Ag7)

Geocode_export_Ag7 <- unique(Geocode_this_Ag7)

write.csv(Geocode_export_Ag7, "geocoding_export_ag7.csv")


#RUnning the TH script over the whole dataset
#1) Identify Australian/OS
#2) Identify Univ, Medical, Govt, Industry, Community, Unknown
#3) Flag the Overseas ones in that list.

