简体   繁体   English

R将时间间隔分成多个周期

[英]R split a time interval to multiple periods

Is there a way to split a time interval, say "2018-01-01" to "2019-01-01", to multiple periods/dates? 有没有一种方法可以将一个时间间隔(例如“ 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")

Thanks in advance. 提前致谢。

Just use Base-R seq on a date to get sequences of dates 只需对日期使用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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM