本文介绍了格式日期时间作为季节在R?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In R, it's possible to format POSIXlt date-time objects as a month:

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

有没有办法做同样的事情季节或3个月的组(DJF,MAM,JJA,SON)?这些部门在气候和生态科学中是非常常见的,而且要像几个月一样快速地进行格式化处理是非常好的。显然,DJF超过2年,但为了这个目的或这个问题,这并不重要 - 只是一直把他们推到任何一年(或者说,理想的情况下,能够指定他们进入哪一年是很好的)

Is there a way to do the same thing with seasons, or 3-month groups (DJF, MAM, JJA, SON)? These divisions are really common in climatological and ecological science, and it would be great to have a neat way to format them quickly like with months. Obviously DJF falls over 2 years, but for the purposes or this question, that doesn't really matter - just consistently shove them into either year, (or, ideally, it would be good to be able to specify which year they go into).

我使用输出作为 by()的索引,所以输出格式不很重要,只要每年/季节都是独一无二的。

I'm using the output as a index for by(), so the output format doesn't matter much, just as long as each year/season is unique.

编辑:示例数据:

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"

应该导致: / p>

should result in:

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

但2015-DJF也可能是2014-DJF。此外,输出的形式并不重要 - 2104q4或201404也会很好。

But the "2015-DJF"s could also be "2014-DJF". Also, the form of the output doesn't matter - "2104q4 or 201404 would also be fine.

推荐答案

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

as.POSIXlt returns a named list (which makes it unsuitable for data.frame columns). The list columns can be individually accessed and include "year" (1900-based, unlike 1970 used for default) and "mon" (0-based). Best place to see this list in hte help system is ?DateTimeClasses:

首先只是一个季节的计算,然后是一个季节计算

First just a Seasons calculation, then a Year-Seasons calculation

 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值进行模数运算。

Shouldn't be that difficult to make a function that constructs the formatting you expect. This is just modulo arithmetic on the POSIXlt values for month and year.

这篇关于格式日期时间作为季节在R?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 22:46