我会创建几个日期。
library(lubridate)
x <- ymd(c("2012-03-26", "2012-05-04", "2012-09-23", "2012-12-31"))
Run Code Online (Sandbox Code Playgroud)
我可以从这些x值中提取年份和季度。
quarter(x, with_year = TRUE, fiscal_start = 10)
[1] 2012.2 2012.3 2012.4 2013.1
但我似乎不能只提取财政年度。这行不通,但什么会呢?
year(x, with_year = TRUE, fiscal_start = 10)
我收到以下错误消息:
年份错误(x,with_year = TRUE,Financial_start = 10):未使用的参数(with_year = TRUE,Financial_start = 10)
如果您不介意额外的步骤,那么您可以提取季度的前 4 个字符以获取年份。
library(lubridate)
x <- ymd(c("2012-03-26", "2012-05-04", "2012-09-23", "2012-12-31"))
q <- quarter(x, with_year = TRUE, fiscal_start = 10)
q
#> [1] 2012.2 2012.3 2012.4 2013.1
fy <- stringr::str_sub(q, 1, 4)
fy
#> [1] "2012" "2012" "2012" "2013"
Run Code Online (Sandbox Code Playgroud)