如何将时间范围分解为每月查询?
how to break a time range into monthly queries?
考虑这个简单的例子
bogus <- function(start_time, end_time){
print(paste('hey this starts on', start_time, 'until', end_time))
}
start_time <- ymd('2018-01-01')
end_time <- ymd('2018-05-01')
> bogus(start_time, end_time)
[1] "hey this starts on 2018-01-01 until 2018-05-01"
不幸的是,在很长的时间范围内这样做不适用于我的现实生活 bogus
功能,所以我需要将我的原始时间范围分成每月的部分。
换句话说,第一个调用是 bogus(ymd('2018-01-01'), ymd('2018-01-31'))
,第二个是 bogus(ymd('2018-02-01'), ymd('2018-02-28'))
,等等
是否有使用 purrr
和 lubridate
的简单方法?
谢谢
带底座:
seqdate<-seq.Date(start_time,end_time,by="1 month")
dateranges<-data.frame(start.dates=seqdate[1:length(seqdate)-1],
end.dates=seqdate[2:length(seqdate)]-1)
start.dates end.dates
1 2018-01-01 2018-01-31
2 2018-02-01 2018-02-28
3 2018-03-01 2018-03-31
4 2018-04-01 2018-04-30
您是否在寻找类似的东西:
library(lubridate)
seq_dates <- seq(start_time, end_time - 1, by = "month")
lapply(seq_dates, function(x) print(paste('hey this starts on', x, 'until', ceiling_date(x, unit = "month") - 1)))
你也可以做一个简短的伪函数,比如:
bogus <- function(start_var, end_var) {
require(lubridate)
seq_dates <- seq(as.Date(start_var), as.Date(end_var) - 1, by = "month")
printed_statement <- lapply(seq_dates, function(x) paste('hey this starts on', x, 'until', ceiling_date(x, unit = "month") - 1))
for (i in printed_statement) { print(i) }
}
并这样称呼它:
bogus("2018-01-01", "2018-05-01")
输出:
[1] "hey this starts on 2018-01-01 until 2018-01-31"
[1] "hey this starts on 2018-02-01 until 2018-02-28"
[1] "hey this starts on 2018-03-01 until 2018-03-31"
[1] "hey this starts on 2018-04-01 until 2018-04-30"
通过这种方式,您可以只给出最短开始日期和最长结束日期,并获得介于两者之间的所有内容。
考虑这个简单的例子
bogus <- function(start_time, end_time){
print(paste('hey this starts on', start_time, 'until', end_time))
}
start_time <- ymd('2018-01-01')
end_time <- ymd('2018-05-01')
> bogus(start_time, end_time)
[1] "hey this starts on 2018-01-01 until 2018-05-01"
不幸的是,在很长的时间范围内这样做不适用于我的现实生活 bogus
功能,所以我需要将我的原始时间范围分成每月的部分。
换句话说,第一个调用是 bogus(ymd('2018-01-01'), ymd('2018-01-31'))
,第二个是 bogus(ymd('2018-02-01'), ymd('2018-02-28'))
,等等
是否有使用 purrr
和 lubridate
的简单方法?
谢谢
带底座:
seqdate<-seq.Date(start_time,end_time,by="1 month")
dateranges<-data.frame(start.dates=seqdate[1:length(seqdate)-1],
end.dates=seqdate[2:length(seqdate)]-1)
start.dates end.dates
1 2018-01-01 2018-01-31
2 2018-02-01 2018-02-28
3 2018-03-01 2018-03-31
4 2018-04-01 2018-04-30
您是否在寻找类似的东西:
library(lubridate)
seq_dates <- seq(start_time, end_time - 1, by = "month")
lapply(seq_dates, function(x) print(paste('hey this starts on', x, 'until', ceiling_date(x, unit = "month") - 1)))
你也可以做一个简短的伪函数,比如:
bogus <- function(start_var, end_var) {
require(lubridate)
seq_dates <- seq(as.Date(start_var), as.Date(end_var) - 1, by = "month")
printed_statement <- lapply(seq_dates, function(x) paste('hey this starts on', x, 'until', ceiling_date(x, unit = "month") - 1))
for (i in printed_statement) { print(i) }
}
并这样称呼它:
bogus("2018-01-01", "2018-05-01")
输出:
[1] "hey this starts on 2018-01-01 until 2018-01-31"
[1] "hey this starts on 2018-02-01 until 2018-02-28"
[1] "hey this starts on 2018-03-01 until 2018-03-31"
[1] "hey this starts on 2018-04-01 until 2018-04-30"
通过这种方式,您可以只给出最短开始日期和最长结束日期,并获得介于两者之间的所有内容。