# Assign the period to group data for time series
date_period <- function(in_date){
require(lubridate)
  
  # Cutting periods in a year. Format 'dd/mm'
  cut1 <- "30/06"
  cut2 <- "31/12"
  
  # There are two cuts. Depending if the cut is in the middle, there could be three or two categories in the same year
  period <- ifelse(in_date <= dmy(paste(cut1,"/",year(in_date),sep='')),dmy(paste(cut1,"/",year(in_date),sep='')),
                   ifelse(in_date <= dmy(paste(cut2,"/",year(in_date),sep='')),dmy(paste(cut2,"/",year(in_date),sep='')),
                          dmy(paste(cut1,"/",year(in_date)+1,sep=''))))
  
  # Returns the day 1 of the period cut
  return(floor_date(as_date(period),unit = 'month'))
}