1

In my dataset I have the following vector of months:

bank$month <-factor(bank$month, 
                       levels = c("jan", "feb", "mar", "apr", "may", "jun",
                                  "jul", "aug", "sep", "oct", "nov", "dec"))

I would like to assign a vector of 4 seasons to each month. I tried case when/ if else (neither of these work). Any suggestions how to solve this issue?

Many thanks, Kasia

petezurich
  • 9,280
  • 9
  • 43
  • 57
  • 2
    *"4 seasons to each month"*? Isn't it the opposite? Please edit the question with an input example and the expected output. – Rui Barradas Dec 01 '19 at 16:41
  • Relevant: [Find which season a particular date belongs to](https://stackoverflow.com/questions/9500114/find-which-season-a-particular-date-belongs-to) – markus Dec 01 '19 at 17:35

2 Answers2

3

The forcats package has a lot of convenience functions for these kinds of factor manipulations. I'd suggest the fct_collapse function, which lets you define levels of a new (less granular) factor based on levels of another factor or character vector:

library(dplyr)
library(forcats)

dates = tibble(
  month = factor(sample(month.abb,40,replace = TRUE),levels = month.abb)
)
dates = dates %>% mutate(
  season = fct_collapse(
    month,
    'Spring' = month.abb[3:5],
    'Summer' = month.abb[6:8],
    'Fall' = month.abb[9:11],
    'Winter' = month.abb[c(12,1,2)]
  )
)

# check them:
table(dates$month,dates$season)

You could do this manually with a switch statement, but why re-invent the wheel!

smarchese
  • 510
  • 2
  • 8
2
library(tidyverse)    
bank %>%
      mutate(Month_No = match(str_to_title(month), month.abb)) %>%
      mutate(Season = case_when(Month_No %in% 3:5 ~ 'Spring',
                                Month_No %in% 6:8 ~ 'Summer',
                                Month_No %in% 9:11 ~ 'Autumn',
                                TRUE ~ 'Winter'))
AlexB
  • 3,061
  • 2
  • 17
  • 19