I need to create a column with bins for which I have found this previous post helpful. However, the bins I want to create, are not simply the grouped intervals of another column, but also need to take factorial data from a second column into account. To be more precise, I want to group fish catches by season and year, i.e. the month in which they were caught into the groups "cold" (Nov-Feb), "warmer" (Mar-Jun) and "warm" (Jul-Oct) and the relevant year.
fish <- data.frame(month = sample(1:12,36,replace=T), year = sample(c(2015,2016,2017),36,replace=T))
fish <- fish[order(fish$year,fish$month),]
library(dplyr)
lev <- c(-Inf, 2, 6, 10, Inf)
lab <- c("cold", "warmer", "warm", "cold")
fish <- mutate(fish,season = cut(month, lev, labels = lab))
The above code (based on the previously mentioned post) will create a bin column. However, I need to associate the Jan/Feb part of the "cold" group with the previous year, i.e. Jan/Feb 2016 should be grouped with 2015's "could" group. Any help will be much appreciated!
Cheers