Hybrid <-function(Projection, NPD.data){

#Inputs are:
#1. The on-roll projection by ward of residence
#2. The ward to ward flows for 2014       
     
##################################################################        
        
#For eachyear calculate growth of population
#defined as difference between that year's population
#and the 2014 baseline population
        

        growth <- select(Projection,1)


        for(i in 2:ncol(Projection)){
        
                growth[i]<-Projection[i]-Projection[2]
              
        }

        
#Import the 2014 distribution of pupils by ward from the roll data
#Column 2 is school ward, column 3 is flow
#Add together flows for each unique school ward


        NPD.data <- select(NPD.data,2,3) %>%
                as.data.table()
        
        NPD.data <- NPD.data[ , lapply(.SD, sum), by = "SchoolWard"] %>%
                as.data.frame()
        

#Join the 2014 roll data to the 2014-23 growth data
#Where there are missing values these will be noted as NA by R
#convert all NA values to zeros

        growth.and.NPD <- merge(growth,NPD.data,by.x="Ward",by.y="SchoolWard",all=T)
        
        growth.and.NPD[is.na(growth.and.NPD)] <- 0



#For each column (year) in the propjection add the 2014 school attendees for the ward
#to the additional resident population in the ward

        last.col <- ncol(growth.and.NPD)
        hybrid <- select(growth.and.NPD,1)

        for(i in 2:(last.col-1)){
        
                hybrid[i] <- growth.and.NPD[i] + growth.and.NPD[last.col]
        
        }

# Sort

        hybrid <- hybrid[order(hybrid[,1]),]


return(hybrid)
       
}