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

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

问题描述

我正在尝试编写一些代码来查看日期,然后将其分配给会计年度.我完全被困住了.

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

有没有什么简单的方法可以做到这一点?

Is there any easy way to do this?

推荐答案

这里有一些替代方案.它们都返回数字年份,但如果您确实需要以 FY 开头的字符串,则使用 paste0("FY", result) 其中 result 是以下任何结果.它们都支持向量输入,即输入的dates可以是一个向量.

Here are some alternatives. They all return numeric years but if you really need a string starting with FY then use paste0("FY", result) where result is any of the results below. They all support vector input, i.e. the input dates can be a vector.

1) zoo::as.yearmon zoo 包有一个 "yearmon" 类,它将年/月表示为年 + 分数,其中分数 = 0 表示一月,2 月 1/12,3 月 2/12,依此类推.

1) zoo::as.yearmon The zoo package has a "yearmon" class which represents year/months as year + fraction where fraction = 0 for jan, 1/12 for feb, 2/12 for march and so on.

使用这个单线就可以了.它减去 4/12(因为四月是年底)并加 1(即加一年).然后获取年份取整数部分:

Using that this one-liner will do it. It subtracts 4/12 (since April is end of year) and adds 1 (i.e. add one year). Then to get the year take the integer part:

library(zoo)

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

2) POSIXlt 这是一个不使用任何包的解决方案.将日期转换为 POSIXlt 类.它的 mo 组件将 Jan 表示为 0,Feb 表示为 1,等等.所以如果我们是 May 或之后(mo 是 4 或更多),那么财政年度是以下日历年,否则为当前日历年.POSIXlt 对象的 year 组件是自 1900 年以来的年数,因此如果我们在 5 月或以后,请将年份添加到 1900 加 1:

2) POSIXlt Here is a solution that does not use any packages. Convert the dates to POSIXlt class. It's mo component represents Jan as 0, Feb as 1, etc. so if we are May or later (mo is 4 or more) then the fiscal year is the following calendar year otherwise it is the current calendar year. The year component of POSIXlt objects is the number of years since 1900 so add the year to 1900 plus 1 if we are at May or later:

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

3) 格式 如果月份大于或等于 5,则将年份添加到 1(否则将其添加为零).这也没有使用包:

3) format Add the year to 1 if the month is greater than or equal to 5 (or to zero if not). This also uses no packages:

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

4) substr.我们可以使用 substr 提取年份,如果提取的月份(也使用 substr 提取)为05"或更大,则转换为数字并加 1.再次没有使用包.

4) substr. We can extract the year using substr, convert to numeric and add 1 if the extracted month (also extracted usingsubstr) is "05" or greater.; Again no packages are used.

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"))

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

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