将时间序列重新排序为自定义 'quasi-chronological' 顺序
Reorder time series into a custom 'quasi-chronological' order
我一直在寻找一种方法来按自定义月份顺序(3 月 - 2 月)而不是按字母顺序、时间顺序、年份等对我的时间序列进行排序。我想保留年份,我有大约 60 年的数据,所以我不能只按月排序。我试过转换为因子和排序,但没有用。这是我的数据片段:
Date GageFlow Month
1 1955-10-01 0.00 10
2 1955-10-02 0.00 10
3 1955-10-03 0.00 10
4 1955-10-04 0.00 10
理想情况下,我希望时间序列从 1956-03-01 开始,并从三月而不是十月开始循环每一天、每一月、每一年。换句话说,日期应该从 1955 年 3 月到 1955 年 12 月排序,然后是 1955 年 1 月到 2 月,然后是 1956 年 3 月到 12 月等等...
为了测试以下代码,我生成了一个包含 2 年数据的虚拟数据框:
Date <- seq(as.Date("1955/1/1"), as.Date("1956/12/31"), by = "day")
GageFlow <- round(runif(731),2)
df <- data.frame(Date, GageFlow, stringsAsFactors = F)
head(df)
Date GageFlow
1 1955-01-01 0.25
2 1955-01-02 0.51
3 1955-01-03 0.13
4 1955-01-04 0.46
5 1955-01-05 0.35
6 1955-01-06 0.20
以下代码根据三月为第一个月重新排列
最后一个是二月。
library(lubridate)
library(dplyr)
# Create month variable
df$month <- month(df$Date)
# Create scaled month variable
df$month_new <- df$month - 2
df$month_new <- ifelse(df$month_new == -1 , 11,
ifelse(df$month_new == 0, 12, df$month_new))
# Rearrange the dataframe
df2 <- df %>% arrange(year(Date), month_new, day(Date)) %>% select(-month_new)
数据集现在具有以下配置:
head(df2)
Date GageFlow month
1 1955-03-01 0.99 3
2 1955-03-02 0.98 3
3 1955-03-03 0.97 3
4 1955-03-04 0.60 3
5 1955-03-05 0.43 3
6 1955-03-06 0.28 3
放大 12 月和 1 月之间的过渡:
df2[305:309,]
Date GageFlow month
305 1955-12-30 0.91 12
306 1955-12-31 0.64 12
307 1955-01-01 0.25 1
308 1955-01-02 0.51 1
309 1955-01-03 0.13 1
放大到次年二月和三月之间的过渡:
df2[364:367,]
Date GageFlow month
364 1955-02-27 0.46 2
365 1955-02-28 0.40 2
366 1956-03-01 0.81 3
367 1956-03-02 0.73 3
您可以使用取模运算符 %%
和偏移量将月份转换为自定义顺序。演示:
一些虚拟数据:
df <- data.frame(Date=seq(as.Date("1955/1/1"), as.Date("1956/12/31"), by = "day"))
现在安排成自定义订单
library(dplyr)
library(lubridate)
df <- arrange(df, year(Date), (month(Date)-3) %% 12)
注意以上假定日期以 "standard" 时间顺序升序开始。如果行一开始没有排序,那么您还需要将月份的日期添加到 arrange
.
df <- arrange(df, year(Date), (month(Date)-3) %% 12, day(Date))
我一直在寻找一种方法来按自定义月份顺序(3 月 - 2 月)而不是按字母顺序、时间顺序、年份等对我的时间序列进行排序。我想保留年份,我有大约 60 年的数据,所以我不能只按月排序。我试过转换为因子和排序,但没有用。这是我的数据片段:
Date GageFlow Month
1 1955-10-01 0.00 10
2 1955-10-02 0.00 10
3 1955-10-03 0.00 10
4 1955-10-04 0.00 10
理想情况下,我希望时间序列从 1956-03-01 开始,并从三月而不是十月开始循环每一天、每一月、每一年。换句话说,日期应该从 1955 年 3 月到 1955 年 12 月排序,然后是 1955 年 1 月到 2 月,然后是 1956 年 3 月到 12 月等等...
为了测试以下代码,我生成了一个包含 2 年数据的虚拟数据框:
Date <- seq(as.Date("1955/1/1"), as.Date("1956/12/31"), by = "day")
GageFlow <- round(runif(731),2)
df <- data.frame(Date, GageFlow, stringsAsFactors = F)
head(df)
Date GageFlow
1 1955-01-01 0.25
2 1955-01-02 0.51
3 1955-01-03 0.13
4 1955-01-04 0.46
5 1955-01-05 0.35
6 1955-01-06 0.20
以下代码根据三月为第一个月重新排列 最后一个是二月。
library(lubridate)
library(dplyr)
# Create month variable
df$month <- month(df$Date)
# Create scaled month variable
df$month_new <- df$month - 2
df$month_new <- ifelse(df$month_new == -1 , 11,
ifelse(df$month_new == 0, 12, df$month_new))
# Rearrange the dataframe
df2 <- df %>% arrange(year(Date), month_new, day(Date)) %>% select(-month_new)
数据集现在具有以下配置:
head(df2)
Date GageFlow month
1 1955-03-01 0.99 3
2 1955-03-02 0.98 3
3 1955-03-03 0.97 3
4 1955-03-04 0.60 3
5 1955-03-05 0.43 3
6 1955-03-06 0.28 3
放大 12 月和 1 月之间的过渡:
df2[305:309,]
Date GageFlow month
305 1955-12-30 0.91 12
306 1955-12-31 0.64 12
307 1955-01-01 0.25 1
308 1955-01-02 0.51 1
309 1955-01-03 0.13 1
放大到次年二月和三月之间的过渡:
df2[364:367,]
Date GageFlow month
364 1955-02-27 0.46 2
365 1955-02-28 0.40 2
366 1956-03-01 0.81 3
367 1956-03-02 0.73 3
您可以使用取模运算符 %%
和偏移量将月份转换为自定义顺序。演示:
一些虚拟数据:
df <- data.frame(Date=seq(as.Date("1955/1/1"), as.Date("1956/12/31"), by = "day"))
现在安排成自定义订单
library(dplyr)
library(lubridate)
df <- arrange(df, year(Date), (month(Date)-3) %% 12)
注意以上假定日期以 "standard" 时间顺序升序开始。如果行一开始没有排序,那么您还需要将月份的日期添加到 arrange
.
df <- arrange(df, year(Date), (month(Date)-3) %% 12, day(Date))