如何将季节开始/结束? [英] How can I round a date to the quarter start/end?

查看:132
本文介绍了如何将季节开始/结束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要采取一个日期的向量,并且每个日期都是下一个季度的第一天。

I need to take a vector of dates and for each date get the first day of the next quarter.

(或者说,到最后一天当前季度,找到下个季度的第一天,开始是我的计划)

(Or rather, to round to the last day of the current quarter, find the first day of the next quarter and take on off is my plan)

lubridate 将圆形/天花板到几个月,但不是季度

lubridate will round/ceiling to months, but not quarters

目前为止,我发现的解决方案是将从1970年到2099年的所有季度开始的矢量列表,然后搜索找到我的日期后的最小日期。显然,这个矢量和比例很差。

Only solution I've found so far is to make a vector listing all the quarter starts from 1970 to 2099 and then search through that to find the minimum date after my date. Clearly, this vectorises and scales badly.

我需要能够指定年底(虽然年份总是从本月1号开始)

I need to be able to specify the year end month (although year always starts on 1st of the month)

例如

x = as.Date("2014-08-15")
RoundToQuarterStart(x, yearStarts = "March")
[1] "2014-09-01"

从这个例子开始,3月1日开始,Q3将从9月1日开始,这是给定日期后的下一个季度开始。 (或等同于此日期属于Q2,截止8月31日)

Since year starts on 1st March in this example, then Q3 starts 1st September, which is the next quarter start after the given date. (or equivalently this date belongs to Q2 which ends on 31st August)

推荐答案

动物园包可以帮助许多与日期相关的事情,包括:

The zoo package can help for many things date-related including this:

library(zoo)

as.yearqtr("2014-08-15", format="%Y-%m-%d")
## [1] "2014 Q3"

as.Date(as.yearqtr("2014-08-15", format="%Y-%m-%d"))
## [1] "2014-07-01"

但是,这可能不会得到你所需要的(有办法从这些值推断出来)。

But, that might not get you what you need (there are ways to extrapolate from those values).

timeDate 包含:

timeFirstDayInQuarter(charvec, format = "%Y-%m-%d", zone = "", FinCenter = "")
timeLastDayInQuarter(charvec, format = "%Y-%m-%d", zone = "", FinCenter = "")

这可能会更容易使用和调整来调整不同的Q1起始起点。

which might make it easer to use and tweak to adjust for different Q1 start origins.

这篇关于如何将季节开始/结束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆