【问题标题】:Reorder time series into a custom 'quasi-chronological' order将时间序列重新排序为自定义的“准时间”顺序
【发布时间】:2016-06-25 00:25:27
【问题描述】:

我一直在寻找一种方法来按自定义月份顺序(3 月 - 2 月)而不是按字母顺序、时间顺序、按年份等对时间序列进行排序。我想保持年份,我有大约 60 年的数据,所以我不能只按月排序。我尝试转换为因子和排序,但没有奏效。这是我的数据的 sn-p:

    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 年 3 月 1 日开始,并从 3 月而不是 10 月开始循环遍历每一天、每一月、每一年。换句话说,日期的顺序应该是从 1955 年 3 月到 1955 年 12 月,然后是 1955 年 1 月到 2 月,然后是 1956 年 3 月到 12 月等等......

【问题讨论】:

  • 我不关注。你想要什么输出?无论年份是从 3 月开始还是从 1 月开始,排序顺序都是相同的。
  • 是按(month-3) %% 12 排序的,你是什么意思? IE。 1955 年 3 月 - 1955 年 12 月,随后是 1955 年 1 月至 2 月,然后是 1956 年 3 月至 12 月等......
  • 如果是 `df$Month2 = (Month-3) %% 12; arrange(df, Year, Month2) 应该这样做
  • @dww 是的,这正是我想要的。当我尝试您的线路时,我收到有关“年份”的错误消息。我看到安排是从 plyr 中提取的……“年”是否需要成为它自己的列或格式化为 lubridate/posit 向量?消息:顺序错误(年,月 2):找不到对象“年”
  • @thelatemail。我不明白为什么我在这个问题上被否决了。是的,我想要的是 df 排序使得第一个月是三月,而不是十月。这会有所不同,因为我每年都在做统计,年份是 3 月到 2 月。

标签: r sorting time-series lubridate


【解决方案1】:

您可以将模运算符 %% 与偏移量一起使用,将月份转换为自定义排序。演示:

一些虚拟数据:

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)

NB 以上假设日期以升序“标准”时间顺序开始。如果一开始没有对行进行排序,那么您还需要将月份中的某天添加到 arrange

df <- arrange(df, year(Date), (month(Date)-3) %% 12, day(Date))

【讨论】:

    【解决方案2】:

    为了测试以下代码,我生成了一个包含 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-28
      • 2010-09-26
      • 2021-03-22
      • 2014-12-20
      • 2018-01-13
      • 1970-01-01
      相关资源
      最近更新 更多