Creating season variable by month with dplyr in R

后端 未结 3 517
渐次进展
渐次进展 2021-01-20 05:12

I have a dataset that has a variable called month, which each month as a character. Is there a way with dplyr to combine some months to create a season variable? I have trie

3条回答
  •  遥遥无期
    2021-01-20 05:40

    You can also try using dplyr::recode or functions from forcats. I think this is the simplest method here:

    library(tidyverse)
    library(lubridate)
    #> 
    #> Attaching package: 'lubridate'
    #> The following object is masked from 'package:base':
    #> 
    #>     date
    data <- tibble(month = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"))
    
    data %>%
      mutate(
        season = fct_collapse(
          .f = month,
          Spring = c("Mar", "Apr", "May"),
          Summer = c("Jun", "Jul", "Aug"),
          Autumn = c("Sep", "Oct", "Nov"),
          Winter = c("Dec", "Jan", "Feb")
        )
      )
    #> # A tibble: 12 x 2
    #>    month season
    #>      
    #>  1 Jan   Winter
    #>  2 Feb   Winter
    #>  3 Mar   Spring
    #>  4 Apr   Spring
    #>  5 May   Spring
    #>  6 Jun   Summer
    #>  7 Jul   Summer
    #>  8 Aug   Summer
    #>  9 Sep   Autumn
    #> 10 Oct   Autumn
    #> 11 Nov   Autumn
    #> 12 Dec   Winter
    

    Created on 2018-04-06 by the reprex package (v0.2.0).

提交回复
热议问题