Determine season from Date using lubridate in R

后端 未结 6 2055
刺人心
刺人心 2020-12-16 04:46

I have a very big dataset with a DateTime Column containing POSIXct-Values. I need to determine the season (Winter - Summer) based on the DateTime

6条回答
  •  天涯浪人
    2020-12-16 05:28

    I packaged @Lars Arne Jordanger's much more elegant approach into a function:

    getTwoSeasons <- function(input.date){
      numeric.date <- 100*month(input.date)+day(input.date)
      ## input Seasons upper limits in the form MMDD in the "break =" option:
      cuts <- base::cut(numeric.date, breaks = c(0,415,1015,1231)) 
      # rename the resulting groups (could've been done within cut(...levels=) if "Winter" wasn't double
      levels(cuts) <- c("Winter", "Summer","Winter")
      return(cuts)
    }
    

    Testing it on some sample data seems to work fine:

    getTwoSeasons(as.POSIXct("2016-01-01 12:00:00")+(0:365)*(60*60*24))
    

提交回复
热议问题