【问题标题】:Extract Fiscal Year with R Lubridate使用 R Lubridate 提取会计年度
【发布时间】:2018-06-05 14:38:20
【问题描述】:

我将创建几个日期。

library(lubridate)
x <- ymd(c("2012-03-26", "2012-05-04", "2012-09-23", "2012-12-31"))

我可以从这些x 值中提取年份和季度。

quarter(x, with_year = TRUE, fiscal_start = 10)

[1] 2012.2 2012.3 2012.4 2013.1

但我似乎无法仅提取财政年度。这不起作用,但什么会?

year(x, with_year = TRUE, fiscal_start = 10)

我收到以下错误消息:

年份错误(x,with_year = TRUE,会计开始 = 10): 未使用的参数(with_year = TRUE, finance_start = 10)

【问题讨论】:

  • as.integer(quarter(x, with_year = TRUE, fiscal_start = 10)) 呢?
  • lubridate::year(x) 为我工作。这就是你要找的东西吗?
  • lubridate::year(x) 适用于日历年。我正在寻找财政年度。

标签: r lubridate


【解决方案1】:

如果您不介意额外的步骤,则可以提取宿舍的前 4 个字符以获得年份。

library(lubridate)

x <- ymd(c("2012-03-26", "2012-05-04", "2012-09-23", "2012-12-31"))

q <- quarter(x, with_year = TRUE, fiscal_start = 10)
q
#> [1] 2012.2 2012.3 2012.4 2013.1

fy <- stringr::str_sub(q, 1, 4)
fy
#> [1] "2012" "2012" "2012" "2013"

【讨论】:

【解决方案2】:
library(lubridate)
library(data.table)

fiscal_start_month = 10

x <- data.table(Dates = ymd(c("2012-03-26", "2012-05-04", "2012-09-23", "2012-12-31")))
x[, Fiscal_Year := ifelse(month(Dates) >= fiscal_start_month, year(Dates) + 1, year(Dates))]

这会产生:

            Dates Fiscal_Year
1: 2012-03-26        2012
2: 2012-05-04        2012
3: 2012-09-23        2012
4: 2012-12-31        2013

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-12
    相关资源
    最近更新 更多