I have a data.frame where I want to create a new column categorising the period from which the sample originates. Each period starts in August 1st and finishes July 31st. For instant, period 1 is 01/Aug/2001 to 31/Jul/2002 and period 2 is 01/Aug/2002 to 31/Jul/2003
I have been trying with dplyr as I want a tidy solution.
The following is an example of the data.frame
Date <- seq(as.Date("2001/08/01"), by = "month", length.out = 60)
AHD <- rnorm(60, mean = 12, sd = 1)
df <- data.frame(Date=Date, AHD = AHD)
I can subset the data.frame into one of the periods using:
df %>%
group_by(dr = cut(Date, breaks = c(range(Date),
as.Date(c("2001-08-01", "2002-07-31"))), include.lowest=TRUE) )
However, I do not know how to do this for a sequence of periods nor how to build the new category column. I need a new column saying whether each row belongs to period 1, period 2 etc.