
output <- function(projection, file.name, indy, col.labels, row.labels, Cohort, WD.OUT){
        
        #Create an output directory if one doesn't already exist
        dir.create(file.path(WD.OUT, Cohort), showWarnings = FALSE)
        
        #Create sub-directories
        dir.create(file.path(WD.OUT, Cohort, "Ward"), showWarnings = FALSE)
        dir.create(file.path(WD.OUT, Cohort, "Borough"), showWarnings = FALSE)
        
        
        
        #Output ward table
        
        #Join the projection file to the row labels and set the column headers
        ward.out <- left_join(row.labels, projection, by="Ward") %>%
                setnames(col.labels)
        
        setwd(file.path(WD.OUT, Cohort, "Ward"))
        nm <- paste(indy, file.name, Cohort,"(Ward).csv")
        
        write.csv(ward.out, nm, row.names=F)
        
        
       
        #Aggregate by boroughs
        
        setwd(file.path(WD.OUT, Cohort, "Borough"))
        
        borough.out <- select(ward.out,-c(1:2))
        
        borough.out <- aggregate(borough.out[c(3:ncol(borough.out))], 
                                 by=list(Borough.Code=borough.out$Borough.Code,
                                         Borough.Name=borough.out$Borough.Name), FUN=sum)
        
        borough.out <- borough.out[order(borough.out$Borough.Code),]
        
        write.csv(borough.out, paste(indy,file.name,Cohort,"(District).csv"), row.names=F)
        
}
        