I have a data set for 10 years. I want to select or subset the data by fiscal year using date variable. For date variable is a character. For instance, I want to select data for fiscal year 2020 which is from 01-10-2019 to 30-09-2020. How can I do this in R ?
            Asked
            
        
        
            Active
            
        
            Viewed 79 times
        
    1
            
            
        - 
                    Please provide sample data (https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info). If a variable is a date, it should really be a proper `Date`-class object in R, it'll help for filtering on inequality (e.g., ranges). – r2evans Oct 10 '21 at 20:07
2 Answers
0
            
            
        Here is an example using zoo package:
df1 <- structure(list(dateA = structure(c(14974, 18628, 14882, 16800, 
17835, 16832, 16556, 15949, 16801), class = "Date")), row.names = c(NA, 
-9L), class = c("tbl_df", "tbl", "data.frame"))
library(dplyr)
library(zoo)
df1 %>% 
  mutate(fiscal_year = as.integer(as.yearmon(dateA) - 4/12 +1))
output:
  dateA      fiscal_year
  <date>           <int>
1 2010-12-31        2011
2 2021-01-01        2021
3 2010-09-30        2011
4 2015-12-31        2016
5 2018-10-31        2019
6 2016-02-01        2016
7 2015-05-01        2016
8 2013-09-01        2014
9 2016-01-01        2016
 
    
    
        TarJae
        
- 72,363
- 6
- 19
- 66
0
            
            
        as said by @r2evans you should post a minimal reprex.
However with the few information you posted maybe this worked example may fit:
date_vect <- c('01-10-2019','30-07-2020','15-07-2019','03-03-2020')
date_vect[substr(date_vect,7,12) == "2020"]
Under the hypothesis that you have a vector of dates in a string format. You may want to pick all the strings with the last four character equal to 2020 (the year you're interested in).
P.S: It's good practice to use the appropriate format when dealing with dates. You can unlock other features such as ordering with R date libraries.
 
    
    
        Alessio
        
- 910
- 7
- 16
