我有类似的问题,但我的公司有一个日历,其中宿舍开始&在不规则日期结束。以下是我在自己的数据中解决这个问题的方法。请注意,我的数据集包含> 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)
谢谢!这是一个非常有用的软件包。还有一种方法可以声明从哪里开始q1?例如,我有时候希望q1在七月份开始。 – Jonny
不是直接。您可以通过映射Q1> Q3等来更改输出。或者查看该函数的源代码并使用移位的输出编写自己的代码。 – Justin