将日期时间格式化为R中的季节?

时间:2014-07-25 02:05:51

标签: r datetime datetime-format

在R中,可以将POSIXlt日期时间对象格式化为一个月:

format(Sys.time(), format='%Y-%m')

有没有办法在季节或3个月组(DJF,MAM,JJA,SON)做同样的事情?这些分歧在气候学和生态科学中非常普遍,如果用几个月的话,用一种简洁的方法来快速形成它们就会很棒。显然DJF超过2年,但出于目的或这个问题,这并不重要 - 只是一直把它们推到任何一年,(或者,理想情况下,能够指定他们走哪一年会很好成)。

我使用输出作为by()的索引,因此输出格式并不重要,只要每年/每季都是唯一的。

编辑:示例数据:

dates <- Sys.Date()+seq(1,380, by=35)
dates <- structure(c(16277, 16312, 16347, 16382, 16417, 16452, 16487, 
                     16522, 16557, 16592, 16627), class = "Date")
dates
#[1] "2014-07-26" "2014-08-30" "2014-10-04" "2014-11-08" "2014-12-13"
# "2015-01-17" "2015-02-21" "2015-03-28" "2015-05-02" "2015-06-06" "2015-07-11"

应该导致:

c("2014-JJA", "2014-JJA", "2014-SON", "2014-SON", "2015-DJF", "2015-DJF", 
  "2015-DJF", "2015-MAM", "2015-MAM", "2015-JJA", "2015-JJA")

但是&#34; 2015-DJF&#34;也可能是&#34; 2014-DJF&#34;。此外,输出的形式并不重要 - &#34; 2104q4或201404也可以。

4 个答案:

答案 0 :(得分:6)

as.POSIXlt返回一个命名列表(这使得它不适合data.frame列)。列表列可以单独访问,包括“year”(基于1900,与1970年默认使用不同)和“mon”(从0开始)。在hte帮助系统中查看此列表的最佳位置是?DateTimeClasses

首先是季节计算,然后是年度季节计算

 c('DJF', 'MAM', 'JJA', 'SON')[ # select from character vector with numeric vector
          1+((as.POSIXlt(dates)$mon+1) %/% 3)%%4]

 [1] "JJA" "JJA" "SON" "SON" "DJF" "DJF" "DJF" "MAM" "MAM" "JJA"
[11] "JJA"



   paste( 1900 + # this is the base year for POSIXlt year numbering 
             as.POSIXlt( dates )$year + 
             1*(as.POSIXlt( dates )$year==12) ,   # offset needed for December
          c('DJF', 'MAM', 'JJA', 'SON')[          # indexing from 0-based-mon
                             1+((as.POSIXlt(dates)$mon+1) %/% 3)%%4] 
          , sep="-")
 [1] "2014-JJA" "2014-JJA" "2014-SON" "2014-SON" "2014-DJF"
 [6] "2015-DJF" "2015-DJF" "2015-MAM" "2015-MAM" "2015-JJA"
[11] "2015-JJA"

创建一个构造您期望的格式的函数应该不难。这只是月和年的POSIXlt值的模运算。

答案 1 :(得分:4)

我喜欢使用查找向量来解决这些问题,例如:

x <- as.POSIXlt(
  seq.Date(as.Date("2000-01-01"),as.Date("2002-01-01"),by="2 months")
)

例如,如果你想指定南半球的季节,你可以这样做:

src <- rep(c("su","au","wi","sp"),each=3)[c(2:12,1)]

paste(format(x,"%Y-%m"),src[x$mon+1])
# [1] "2000-01 su" "2000-03 au" "2000-05 au" "2000-07 wi" "2000-09 sp"
# [6] "2000-11 sp" "2001-01 su" "2001-03 au" "2001-05 au" "2001-07 wi"
#[11] "2001-09 sp" "2001-11 sp" "2002-01 su"

根据需要更改src名称以重新标记类别。

答案 2 :(得分:2)

让Q1成为DJF; Q2,MAM;等等:

seasonal.quarters <- function(x) {
    x <- as.POSIXlt(x)
    x$mon <- (x$mon + 1) %% 12
    quarters(x)
}

options(stringsAsFactors=FALSE)

nonleap.year <- seq(from=as.POSIXct('2013-1-1'), to=as.POSIXct('2014-1-1'), by='day')
d <- data.frame(ms=months(nonleap.year), qs=seasonal.quarters(nonleap.year))
by(d, INDICES=list(d$qs), FUN=function(x) unique(x$ms))
# : Q1
# [1] "January"  "February" "December"
# -------------------------------------
# : Q2
# [1] "March" "April" "May"  
# -------------------------------------
# : Q3
# [1] "June"   "July"   "August"
# -------------------------------------
# : Q4
# [1] "September" "October"   "November" 

leap.year <- seq(from=as.POSIXct('2016-1-1'), to=as.POSIXct('2017-1-1'), by='day')
d <- data.frame(ms=months(leap.year), qs=seasonal.quarters(leap.year))
by(d, INDICES=list(d$qs), FUN=function(x) unique(x$ms))
# : Q1
# [1] "January"  "February" "December"
# -------------------------------------
# : Q2
# [1] "March" "April" "May"  
# -------------------------------------
# : Q3
# [1] "June"   "July"   "August"
# -------------------------------------
# : Q4
# [1] "September" "October"   "November" 

答案 3 :(得分:2)

这是上述42-'s answer的替代方案。我在那里看到我的评论是因为我发布了它。

dates_orig <- as.POSIXlt(c("2013-01-01", "2013-02-01", "2013-03-01", "2013-04-01", "2013-05-01", "2013-06-01", "2013-07-01", "2013-08-01", "2013-09-01", "2013-10-01", "2013-11-01", "2013-12-01", "2014-01-01", "2014-02-01", "2014-03-01", "2014-04-01", "2014-05-01", "2014-06-01", "2014-07-01", "2014-08-01", "2014-09-01", "2014-10-01", "2014-11-01", "2014-12-01"))

format(dates_orig, format='%Y%b')
 [1] "2013Jan" "2013Feb" "2013Mar" "2013Apr" "2013May" "2013Jun" "2013Jul" "2013Aug" "2013Sep" "2013Oct" "2013Nov" "2013Dec" "2014Jan" "2014Feb" "2014Mar"
[16] "2014Apr" "2014May" "2014Jun" "2014Jul" "2014Aug" "2014Sep" "2014Oct" "2014Nov" "2014Dec"

dates <- as.POSIXlt(dates_orig)
# shift Jan and Feb to the previous year
dates$year[dates$mon < 2] <- dates$year[dates$mon < 2] - 1
# convert months to seasons (named by first month of season)
dates$mon <- (((dates$mon - 2) %/% 3) %% 4) * 3 + 2

format(dates, format='%Y%b')
[1] "2012Dec" "2012Dec" "2013Mar" "2013Mar" "2013Mar" "2013Jun" "2013Jun" "2013Jun" "2013Sep" "2013Sep" "2013Sep" "2013Dec" "2013Dec" "2013Dec" "2014Mar"
[16] "2014Mar" "2014Mar" "2014Jun" "2014Jun" "2014Jun" "2014Sep" "2014Sep" "2014Sep" "2014Dec"