问题
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.Date
gives 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