我遇到了类似的问题,但我的公司有一个日历,其中季度开始和结束的日期不规则。以下是我如何在自己的数据中解决这个问题。请注意,我的数据集包含 > 5MM 行,因此我使用data.table
而不是 data.frame。
# My data is contained in the myDT data.table.
# Dates are contained in the date column.
require("data.table")
Q1FY14 <- myDT[ which(date >= "2013-02-02" & date <= "2013-05-03"), ]
Q2FY14 <- myDT[ which(date >= "2013-05-04" & date <= "2013-08-02"), ]
Q3FY14 <- myDT[ which(date >= "2013-08-03" & date <= "2013-11-01"), ]
Q4FY14 <- myDT[ which(date >= "2013-11-02" & date <= "2014-01-31"), ]
Q1FY15 <- myDT[ which(date >= "2014-02-01" & date <= "2014-05-02"), ]
# Create new vectors.
Q1.14 <- rep("Q1 FY14", nrow(Q1FY14))
Q2.14 <- rep("Q2 FY14", nrow(Q2FY14))
Q3.14 <- rep("Q3 FY14", nrow(Q3FY14))
Q4.14 <- rep("Q4 FY14", nrow(Q4FY14))
Q1.15 <- rep("Q1 FY15", nrow(Q1FY15))
# Add each of my new vectors to their associate data.table.
Q1FY14$quarter <- Q1.14
Q2FY14$quarter <- Q2.14
Q3FY14$quarter <- Q3.14
Q4FY14$quarter <- Q4.14
Q1FY15$quarter <- Q1.15
# Bring it all together.
newDT <- rbind(Q1FY14, Q2FY14)
newDT <- rbind(newDT, Q3FY14)
newDT <- rbind(newDT, Q4FY14)
newDT <- rbind(newDT, Q1FY15)
# Clean up data.
rm(Q1FY14, Q2FY14, Q3FY14, Q4FY14, Q1FY15, Q1.14, Q2.14, Q3.14, Q4.14, Q1.15)
这为每一行添加了正确的季度。我需要进行一些其他的小调整才能使其可用。
# Change the column order so that quarter appears next to date.
setcolorder(newDT, c("date", "quarter", ...))
# Change the quarter column to factors.
newDT$quarter <- factor(newDT$quarter)