计算下一个季度何时开始

发布于 2025-01-20 05:00:37 字数 500 浏览 0 评论 0原文

看看这个:

> as.Date("2000-01-01")+months(1)
[1] "2000-02-01"

> as.Date("2000-01-01")+months(3)
[1] "2000-04-01"

> as.Date("2000-01-01")+months(24)
[1] "2002-01-01"

我想要相同的但对于季度,当我将 p 季度添加到日期时,我想知道下一个季度的开始日期,如下所示,但我收到此错误:

as.Date("2000-01-01")+quarters(1)

UseMethod("quarters") 中的错误:没有适用的方法 'quarters' 应用于类“c('double', 'numeric')”的对象

我该怎么做?我确实需要使用与 months() 不同的东西。

Have a look at this:

> as.Date("2000-01-01")+months(1)
[1] "2000-02-01"

> as.Date("2000-01-01")+months(3)
[1] "2000-04-01"

> as.Date("2000-01-01")+months(24)
[1] "2002-01-01"

I want the same but for quarters, I want to know the date the next quarter starts when I add p quarters to a date, like this, but I get this error:

as.Date("2000-01-01")+quarters(1)

Error in UseMethod("quarters") : no applicable method for
'quarters' applied to an object of class "c('double', 'numeric')"

How can I do it? I really need to use something different from months().

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

机场等船 2025-01-27 05:00:37

您可以为此使用时钟的year_quarter_day类。这是它设计的东西之一。

library(clock)
library(magrittr)

x <- as.Date("2000-01-01")
x
#> [1] "2000-01-01"

# Uses January as the quarter start
x <- as_year_quarter_day(x)
x
#> <year_quarter_day<January><day>[1]>
#> [1] "2000-Q1-01"

x + duration_quarters(1)
#> <year_quarter_day<January><day>[1]>
#> [1] "2000-Q2-01"
as.Date(x + duration_quarters(1))
#> [1] "2000-04-01"

x + duration_quarters(3)
#> <year_quarter_day<January><day>[1]>
#> [1] "2000-Q4-01"
as.Date(x + duration_quarters(3))
#> [1] "2000-10-01"

x + duration_quarters(24)
#> <year_quarter_day<January><day>[1]>
#> [1] "2006-Q1-01"
as.Date(x + duration_quarters(24))
#> [1] "2006-01-01"

# Say you were at some arbitrary date in the quarter
date <- as.Date("2020-05-10")
date
#> [1] "2020-05-10"

# If you want to know the start of the quarter you can use
# calendar_start()
date %>%
  as_year_quarter_day() %>%
  calendar_start("quarter") %>%
  as.Date()
#> [1] "2020-04-01"

如果您真的只是在寻找下一个季度的开始,那么最强大的事情是:更改为季度精度,添加四分之一,将一天设置为1,迄今为止转换。

library(clock)
library(magrittr)

date <- as.Date("2020-05-10")
date
#> [1] "2020-05-10"

date %>%
  as_year_quarter_day() %>% # "2020-Q2-40"
  calendar_narrow("quarter") %>% # "2020-Q2"
  add_quarters(1) %>% # "2020-Q3"
  set_day(1) %>% # "2020-Q3-01"
  as.Date()
#> [1] "2020-07-01"

You can use clock's year_quarter_day class for this. It is one of the things it was designed for.

library(clock)
library(magrittr)

x <- as.Date("2000-01-01")
x
#> [1] "2000-01-01"

# Uses January as the quarter start
x <- as_year_quarter_day(x)
x
#> <year_quarter_day<January><day>[1]>
#> [1] "2000-Q1-01"

x + duration_quarters(1)
#> <year_quarter_day<January><day>[1]>
#> [1] "2000-Q2-01"
as.Date(x + duration_quarters(1))
#> [1] "2000-04-01"

x + duration_quarters(3)
#> <year_quarter_day<January><day>[1]>
#> [1] "2000-Q4-01"
as.Date(x + duration_quarters(3))
#> [1] "2000-10-01"

x + duration_quarters(24)
#> <year_quarter_day<January><day>[1]>
#> [1] "2006-Q1-01"
as.Date(x + duration_quarters(24))
#> [1] "2006-01-01"

# Say you were at some arbitrary date in the quarter
date <- as.Date("2020-05-10")
date
#> [1] "2020-05-10"

# If you want to know the start of the quarter you can use
# calendar_start()
date %>%
  as_year_quarter_day() %>%
  calendar_start("quarter") %>%
  as.Date()
#> [1] "2020-04-01"

If you are really just looking for the start of the next quarter, the most robust thing to do is to: change to a quarterly precision, add a quarter, set the day to 1, convert back to date.

library(clock)
library(magrittr)

date <- as.Date("2020-05-10")
date
#> [1] "2020-05-10"

date %>%
  as_year_quarter_day() %>% # "2020-Q2-40"
  calendar_narrow("quarter") %>% # "2020-Q2"
  add_quarters(1) %>% # "2020-Q3"
  set_day(1) %>% # "2020-Q3-01"
  as.Date()
#> [1] "2020-07-01"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文