Creating season variable by month with dplyr in R

后端 未结 3 515
渐次进展
渐次进展 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:26

    When there are multiple key/value, we can do a join with a key/val dataset

    keyval <- data.frame(month = month.abb, 
          season = rep(c("Winter", "Spring", "Summer", "Fall"), each = 3),
          stringsAsFactors = FALSE)
    
    left_join(data, keyval)
    
    0 讨论(0)
  • 2021-01-20 05:39

    The correct syntax should be

    data %>% mutate(season = ifelse(month %in% 10:12, "Fall",
                                   ifelse(month %in% 1:3, "Winter",
                                          ifelse(month %in% 4:6, "Spring",
                                                 "Summer"))))
    

    Edit: probably a better way to get the job done

    Astronomical Seasons

    temp_data %>%
      mutate(
        season = case_when(
          month %in% 10:12 ~ "Fall",
          month %in%  1:3  ~ "Winter",
          month %in%  4:6  ~ "Spring",
          TRUE ~ "Summer"))
    

    Meteorological Seasons

    temp_data %>%
      mutate(
        season = case_when(
          month %in%  9:11 ~ "Fall",
          month %in%  c(12, 1, 2)  ~ "Winter",
          month %in%  3:5  ~ "Spring",
          TRUE ~ "Summer"))
    
    0 讨论(0)
  • 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
    #>    <chr> <fct> 
    #>  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).

    0 讨论(0)
提交回复
热议问题