You will need tidyverse and lubidate package.
if(!require(tidyverse)) install.packages("tidyverse", repos = "http://cran.us.r-project.org")
if(!require(lubridate)) install.packages("lubridate", repos = "http://cran.us.r-project.org")
Using sample dates use fiscal_state function. It will only work for quarter, and not for year.
x <- ymd(c("2009-04-01", "2011-06-03", "2007-10-02", "2012-12-25"))
quarter <- quarter(x, with_year = TRUE, fiscal_start = 9)
print(quarter)
## [1] 2009.3 2011.4 2008.1 2012.2
Now you can use str_sub to pull the fiscal year only.
fiscal_year <- stringr::str_sub(quarter, 1, 4)
print(fiscal_year)
## [1] "2009" "2011" "2008" "2012"
Putting it all together in a table
data.frame(x, fiscal_year)
## x fiscal_year
## 1 2009-04-01 2009
## 2 2011-06-03 2011
## 3 2007-10-02 2008
## 4 2012-12-25 2012
Fiscal Year with one line of code
str_sub(quarter(x, with_year = TRUE, fiscal_start = 10), 1, 4)
## [1] "2009" "2011" "2008" "2013"