【问题标题】:R split a time interval to multiple periodsR将时间间隔分成多个时段
【发布时间】:2019-02-12 11:36:03
【问题描述】:

有没有办法将时间间隔(例如“2018-01-01”到“2019-01-01”)拆分为多个时段/日期?

if breaks = "1 month" then it's c("2018-01-01","2018-02-01", ... )
if breaks = "1 quarter" then it's c("2018-01-01","2018-04-01", ... )
if breaks = "1 half yearly" then it's c("2018-01-01","2018-07-01", ... )
if breaks = "1 year" then it's c("2018-01-01","2019-01-01")

提前致谢。

【问题讨论】:

    标签: r time split intervals period


    【解决方案1】:

    只需在日期上使用 Base-R seq 即可获取日期序列

    from <- as.Date( "2018-01-01" )
    to   <- as.Date( "2019-01-01" )                
    
    seq( from, to, by = "1 month")
    # [1] "2018-01-01" "2018-02-01" "2018-03-01" "2018-04-01" "2018-05-01" "2018-06-01" "2018-07-01" "2018-08-01" "2018-09-01" "2018-10-01"
    # [11] "2018-11-01" "2018-12-01" "2019-01-01"
    
    seq( from, to, by = "1 quarter")
    #[1] "2018-01-01" "2018-04-01" "2018-07-01" "2018-10-01" "2019-01-01"
    
    seq( from, to, by = "6 month")
    #[1] "2018-01-01" "2018-07-01" "2019-01-01"
    
    seq( from, to, by = "1 year")
    # [1] "2018-01-01" "2019-01-01"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-20
      • 1970-01-01
      • 2012-07-23
      相关资源
      最近更新 更多