使用 Tidyverse 和 Lubridate 整理不规则宿舍(从 7 月开始)
Sorting out irregular quarters (starting in July) using Tidyverse and Lubridate
我目前正在尝试生成(变异)两列:
- “NewThisYear”表示发生在当前财政年度的观察结果
- “NewThisQuarter”表示当前季度发生的观察结果
我通常的方法涉及附加的代码。此代码适用于 1 月份开始的财政年度,但我无法弄清楚从 2021 年开始的前两个季度提取下一年(即 2022 年)的好方法。最好解决这个问题的方法是在季度为 1|2 时将 + 1 添加到年份,或者是否有更简单的方法来实现这一点?
感谢指点!
代码:
Calendar <- structure(list(Date = structure(c(18780, 18787, 18794, 18801,
18808, 18815, 18822, 18829, 18836, 18843, 18850, 18857, 18864,
18871, 18878, 18885, 18892, 18899, 18906, 18913, 18920, 18927,
18934, 18941, 18948, 18955, 18962, 18969, 18976, 18983, 18990,
18997, 19004, 19011, 19018, 19025, 19032), class = "Date"), NameMonthShort = structure(c(6L,
6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L,
9L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L,
12L, 1L, 1L, 1L, 1L, 2L, 2L), .Label = c("Jan", "Feb", "Mar",
"Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
), class = c("ordered", "factor")), Year = c(2021, 2021, 2021,
2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021,
2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021,
2021, 2021, 2021, 2021, 2021, 2021, 2022, 2022, 2022, 2022, 2022,
2022), Quarter = c(4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L), QuarterYear = c("4-2021",
"4-2021", "4-2021", "4-2021", "4-2021", "1-2021", "1-2021", "1-2021",
"1-2021", "1-2021", "1-2021", "1-2021", "1-2021", "1-2021", "1-2021",
"1-2021", "1-2021", "1-2021", "2-2021", "2-2021", "2-2021", "2-2021",
"2-2021", "2-2021", "2-2021", "2-2021", "2-2021", "2-2021", "2-2021",
"2-2021", "2-2021", "3-2022", "3-2022", "3-2022", "3-2022", "3-2022",
"3-2022")), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-37L))
Calendar <- Calendar %>%
mutate(
NewThisYear = case_when(
Year == year(Sys.Date()) ~ "Yes",
TRUE ~ as.character("No")
),
NewThisQuarter = case_when(
Year == year(Sys.Date()) & Quarter == quarter(Sys.Date(), with_year = F, fiscal_start = 7) ~ "Yes",
TRUE ~ as.character("No")
)
)
创建一些方便的函数可能会更简单一些:
library(lubridate); library(dplyr)
FY <- function(x) { year(x %m+% months(6)) } # EDIT from %m-%
Qtr = function(x) { floor_date(x, "quarter") }
Calendar %>%
mutate(qtr = Qtr(Date), FY = FY(Date),
NewThisYear = FY(Date) == FY(Sys.Date()),
NewThisQuarter = Qtr(Date) == Qtr(Sys.Date()))
我目前正在尝试生成(变异)两列:
- “NewThisYear”表示发生在当前财政年度的观察结果
- “NewThisQuarter”表示当前季度发生的观察结果
我通常的方法涉及附加的代码。此代码适用于 1 月份开始的财政年度,但我无法弄清楚从 2021 年开始的前两个季度提取下一年(即 2022 年)的好方法。最好解决这个问题的方法是在季度为 1|2 时将 + 1 添加到年份,或者是否有更简单的方法来实现这一点?
感谢指点!
代码:
Calendar <- structure(list(Date = structure(c(18780, 18787, 18794, 18801,
18808, 18815, 18822, 18829, 18836, 18843, 18850, 18857, 18864,
18871, 18878, 18885, 18892, 18899, 18906, 18913, 18920, 18927,
18934, 18941, 18948, 18955, 18962, 18969, 18976, 18983, 18990,
18997, 19004, 19011, 19018, 19025, 19032), class = "Date"), NameMonthShort = structure(c(6L,
6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L,
9L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L,
12L, 1L, 1L, 1L, 1L, 2L, 2L), .Label = c("Jan", "Feb", "Mar",
"Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
), class = c("ordered", "factor")), Year = c(2021, 2021, 2021,
2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021,
2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021,
2021, 2021, 2021, 2021, 2021, 2021, 2022, 2022, 2022, 2022, 2022,
2022), Quarter = c(4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L), QuarterYear = c("4-2021",
"4-2021", "4-2021", "4-2021", "4-2021", "1-2021", "1-2021", "1-2021",
"1-2021", "1-2021", "1-2021", "1-2021", "1-2021", "1-2021", "1-2021",
"1-2021", "1-2021", "1-2021", "2-2021", "2-2021", "2-2021", "2-2021",
"2-2021", "2-2021", "2-2021", "2-2021", "2-2021", "2-2021", "2-2021",
"2-2021", "2-2021", "3-2022", "3-2022", "3-2022", "3-2022", "3-2022",
"3-2022")), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-37L))
Calendar <- Calendar %>%
mutate(
NewThisYear = case_when(
Year == year(Sys.Date()) ~ "Yes",
TRUE ~ as.character("No")
),
NewThisQuarter = case_when(
Year == year(Sys.Date()) & Quarter == quarter(Sys.Date(), with_year = F, fiscal_start = 7) ~ "Yes",
TRUE ~ as.character("No")
)
)
创建一些方便的函数可能会更简单一些:
library(lubridate); library(dplyr)
FY <- function(x) { year(x %m+% months(6)) } # EDIT from %m-%
Qtr = function(x) { floor_date(x, "quarter") }
Calendar %>%
mutate(qtr = Qtr(Date), FY = FY(Date),
NewThisYear = FY(Date) == FY(Sys.Date()),
NewThisQuarter = Qtr(Date) == Qtr(Sys.Date()))