簡體   English   中英

將日期分配到會計年度

[英]Assigning Dates to Fiscal Year

我正在嘗試提供一些代碼來查看日期,然后將其分配給會計年度。 我完全陷入了困境。

我有一個包含POSIXct格式的日期的變量:

df$Date
#2015-05-01 CST
#2015-04-30 CST
#2014-09-01 CST

我需要做的是采取這些日期並返回從5月1日到4月30日的財政年度。例如,2016財年運行2015-05-01到2016-04-30。 結果看起來像這樣:

df$Date                df$FiscalYear
#2015-05-01 CST        #FY2016
#2015-04-30 CST        #FY2015
#2014-09-01 CST        #FY2015

有沒有簡單的方法來做到這一點?

這是一些替代方案。 它們都返回數字年份,但是如果你真的需要一個以FY開頭的字符串,那么使用paste0("FY", result) ,其中result是以下任何結果。 它們都支持矢量輸入,即輸入dates可以是矢量。

1)zoo :: as.yearmon動物園包有一個"yearmon"類,它代表年/月為年份+分數,其中分數= 0表示jan,1/12表示feb,2/12表示3月等等。

使用這個單行將會做到這一點。 它減去4/12(自4月以來年末)並增加1(即增加一年)。 然后獲取年份取整數部分:

library(zoo)

as.integer(as.yearmon(dates) - 4/12 + 1)
## [1] 2016 2015 2015

2)POSIXlt這是一個不使用任何包的解決方案。 將日期轉換為POSIXlt類。 它的mo分量表示Jan為0,2月為1,等等。因此,如果我們是5月或更晚( mo為4或更高),那么會計年度是下一個日歷年,否則它是當前日歷年。 POSIXlt對象的year組件是自1900年以來的年數,因此如果我們在5月或更晚,則將年份添加到1900加1:

lt <- as.POSIXlt(dates)
lt$year + (lt$mo >= 4) + 1900
## [1] 2016 2015 2015

3)格式如果月份大於或等於5,則將年份添加到1(如果不是,則將年份添加到零)。 這也不使用包:

as.numeric(format(dates, "%Y")) + (format(dates, "%m") >= "05")
## [1] 2016 2015 2015

4)substr 我們可以使用substr提取年份,轉換為數字,如果提取的月份(也使用substr提取)為“05”或更大,則添加1。 同樣沒有使用包。

as.numeric(substr(dates, 1, 4)) + (substr(dates, 6, 7) >= "05")
## [1] 2016 2015 2015

5)read.table這也不使用包。

with(read.table(text = format(dates), sep = "-"), V1 + (V2 >= 5))
## [1] 2016 2015 2015

注意:我們使用此作為輸入dates

dates <- as.Date(c("2015-05-01", "2015-04-30", "2014-09-01"))

您可以將seq與POSIXct對象一起使用,以生成跨越數據的年份的“cutpoints”列表或會計年度的第1天,然后使用findInterval計算特定日期所屬的時間間隔:

> dates <- as.POSIXct( c('2015-05-01','2015-04-30','2014-09-01'))
> fy.tmp <- seq( as.POSIXct('2000-05-01'), length=25, by='year')
> fiscalYear <- (2001:2025)[ findInterval(dates,fy.tmp) ]
> fiscalYear
[1] 2016 2015 2015

如果您想要一個因子作為結果,您也可以使用cut函數而不是findInterval

加入G.格洛騰迪克的好回應。 使用lubridate

year(dates) + (month(dates) >= 5)

嘗試修改這個:

Federal.FY <- function(x,firstMonth=10,  # I've altered this line to follow the federal fiscal year, October
                       fy.prefix='FY',
                       quarter.prefix='Q',
                       sep='-',
                       level.range=c(min(x), max(x)) ) {if(level.range[1] > min(x) | level.range[2] < max(x)) {
warning(paste0('The range of x is greater than level.range. Values ',
               'outside level.range will be returned as NA.'))}
quarterString <- function(d) {
year <- as.integer(format(d, format='%Y'))
month <- as.integer(format(d, format='%m'))
y <- ifelse(firstMonth > 1 & month >= firstMonth, year+1, year)
q <- cut( (month - firstMonth) %% 12, breaks=c(-Inf,2,5,8,Inf),
          labels=paste0(quarter.prefix, 1:4))
return(paste0(fy.prefix, y, sep, q))}
vals <- quarterString(x)
levels <- unique(quarterString(seq(
as.Date(format(level.range[1], '%Y-%m-01')),
as.Date(format(level.range[2], '%Y-%m-28')), by='month')))
return(factor(vals, levels=levels, ordered=TRUE))}

d <- as.Date("2016-10-02")
Federal.FY(d)
##[1] FY2017-Q1
##Levels: FY2017-Q1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM