0

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

M.Teich
  • 575
  • 5
  • 22

2 Answers2

0

This will do the trick:

fish <- mutate(fish, season = case_when(month %in% c(1, 2, 11, 12) ~ "cold",
                                        month %in% c(3, 4, 5, 6) ~ "warmer",
                                        TRUE ~ "warm"))
phiver
  • 23,048
  • 14
  • 44
  • 56
  • phiver, thanks for your answer, but I think your code produces what I already have. On second thought, I think I need to create a second "year" column, running from March to February. – M.Teich Jan 26 '18 at 14:20
0

Could have thought of that earlier: Simply adding another "year" column, where Jan/Feb get a value of "year"-1 will do.

fish <- mutate(fish,seasonYY = ifelse(fish$month==1|fish$month==2,fish$year-1,fish$year))

Later analyses can use "new.year" and "season" to subset for the right catches.

M.Teich
  • 575
  • 5
  • 22