生成具有特定开始和结束日期的时间序列

发布于 2024-12-07 07:48:04 字数 147 浏览 1 评论 0原文

我想生成一个时间序列,其中包含范围内的所有业务日期:

startDate = "1990-01-01"
endDate = "1990-12-31"

例如“1990-01-01”,“1990-01-02”,...

I want to generate a time series with all business dates in the range:

startDate = "1990-01-01"
endDate = "1990-12-31"

For example "1990-01-01", "1990-01-02", ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

待天淡蓝洁白时 2024-12-14 07:48:04

@csgillespie:chron提供了is.weekend函数:

days = seq(as.Date("1990-01-01"), as.Date("1990-12-31"), by="1 day")
library(chron)
weekDays = days[!is.weekend(days)]

## let's check the result with the function weekdays
weekdays(weekDays)

此外,使用format无需chron也可以获得相同的结果>:

isWeekend <- function(x) {format(x, '%w') %in% c(0, 6)}
weekDays2 = days[!isWeekend(days)]

@csgillespie: chron provides the function is.weekend:

days = seq(as.Date("1990-01-01"), as.Date("1990-12-31"), by="1 day")
library(chron)
weekDays = days[!is.weekend(days)]

## let's check the result with the function weekdays
weekdays(weekDays)

Besides, you can get the same results without chron using format:

isWeekend <- function(x) {format(x, '%w') %in% c(0, 6)}
weekDays2 = days[!isWeekend(days)]
红ご颜醉 2024-12-14 07:48:04

您只需使用 seq 命令即可。例如,

##Specify you want 10 dates starting on 1990-01-01
R> seq(as.Date("1990-01-01"), length.out=10, by="1 day")
 [1] "1990-01-01" "1990-01-02" "1990-01-03" "1990-01-04" "1990-01-05"
 [6] "1990-01-06" "1990-01-07" "1990-01-08" "1990-01-09" "1990-01-10"

或者

##Specify the start and end with increment
R> seq(as.Date("1990-01-01"), as.Date("1990-01-10"), by="1 day")
 [1] "1990-01-01" "1990-01-02" "1990-01-03" "1990-01-04" "1990-01-05"
 [6] "1990-01-06" "1990-01-07" "1990-01-08" "1990-01-09" "1990-01-10"

要获取工作日,您可以使用 chron 库:

days = seq(as.Date("1990-01-01"), as.Date("1990-12-31"), by="1 day")
library(chron)
weekDays = days[!is.weekend(days)]

You can just use the seq command. For example,

##Specify you want 10 dates starting on 1990-01-01
R> seq(as.Date("1990-01-01"), length.out=10, by="1 day")
 [1] "1990-01-01" "1990-01-02" "1990-01-03" "1990-01-04" "1990-01-05"
 [6] "1990-01-06" "1990-01-07" "1990-01-08" "1990-01-09" "1990-01-10"

or

##Specify the start and end with increment
R> seq(as.Date("1990-01-01"), as.Date("1990-01-10"), by="1 day")
 [1] "1990-01-01" "1990-01-02" "1990-01-03" "1990-01-04" "1990-01-05"
 [6] "1990-01-06" "1990-01-07" "1990-01-08" "1990-01-09" "1990-01-10"

To just get business days, you can use the chron library:

days = seq(as.Date("1990-01-01"), as.Date("1990-12-31"), by="1 day")
library(chron)
weekDays = days[!is.weekend(days)]
我偏爱纯白色 2024-12-14 07:48:04

有一个名为 ?weekdaysbase 函数。

startDate = "1990-01-01"
endDate = "1990-12-31"

x <- seq(as.Date(startDate), to = as.Date(endDate), by="1 day")

x[!weekdays(x) %in% c("Sunday", "Saturday")]

但是,由于日期的实际名称将特定于区域设置,因此请务必正确设置它们。

请注意,weekdays 只是 format(x, "%A") 的包装。有关格式代码的详细信息,请参阅 ?strptime

There is a base function called ?weekdays.

startDate = "1990-01-01"
endDate = "1990-12-31"

x <- seq(as.Date(startDate), to = as.Date(endDate), by="1 day")

x[!weekdays(x) %in% c("Sunday", "Saturday")]

But, since the actual names of the days will be locale-specific, be sure to set those correctly.

Note that weekdays is just a wrapper on format(x, "%A"). See ?strptime for the details on the format codes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文