Forcing end of quarter date for as.Date(as.yearqtr())

ⅰ亾dé卋堺 提交于 2020-08-07 09:53:31

问题


as.yearqtr() from the zoo package appears to use the beginning of the quarter:

library(zoo)
x <- "2015-05-17"
x <- as.Date(x)
x <- as.Date(as.yearqtr(x))
x

# [1] "2015-04-01"

How might I instead grab the end of quarter?

x

# [1] "2015-06-30"

Thanks!


回答1:


Use frac = 1 as shown:

x <- as.Date("2015-05-17")
as.Date( as.yearqtr(x), frac = 1 )

giving:

[1] "2015-06-30"



回答2:


library(zoo)

x <- as.Date("2015-05-17")
x <- as.Date(as.yearqtr(x)+0.25)-1

x
# [1] "2015-06-30"

as.yearqtr(x)+0.25 gives you the next quarter. as.Dategives you the first day in this next quarter. -1 delivers the last day of the original quarter.



来源:https://stackoverflow.com/questions/31391271/forcing-end-of-quarter-date-for-as-dateas-yearqtr

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!