将日期分配给会计年度 [英] Assigning Dates to Fiscal Year

查看:120
本文介绍了将日期分配给会计年度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试一些代码,看一个日期,然后将其分配到一个会计年度。我完全卡住了

I'm trying to come up with some code that will look at a date and then assign it to a fiscal year. I'm totally stuck.

我有一个包含POSIXct格式日期的变量:

I have a variable that contains dates in POSIXct format:

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。结果如下所示:

What I need to be able to do is take those dates and return a fiscal year, which runs from May 1 - April 30. For example, Fiscal Year 2016 runs 2015-05-01 through 2016-04-30. Results would look something like this:

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

有没有办法这样做?

推荐答案

您可以使用 seq 与POSIXct对象生成切点列表或跨越数据的年度的会计年度的第一天,然后使用 findInterval 计算特定日期属于哪一个间隔:

You can use seq with POSIXct objects to generate a list of the "cutpoints" or 1st day of the fiscal year for the years spanning your data, then use findInterval to compute which of the intervals a particular date falls into:

> 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 如果你想要一个因素作为结果。

You could also use the cut function instead of findInterval if you want a factor as the result.

这篇关于将日期分配给会计年度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆