R seq.Date() Function - Generating Date Sequences
The R seq.Date() function is used to generate sequences of dates at regular intervals.
In time series analysis and periodic report generation, creating date sequences is a very common requirement.
The syntax of the seq.Date() function is as follows:
seq(from, to, by = "days")
Parameter Description:
-
from The start date.
-
to The end date.
-
by The interval: "days", "weeks", "months", "years", or "3 days", etc.
Example
This week's date sequence
start
<-
as.Date
(
"2026-05-11"
)
week_dates
<-
seq
(
start
,
by
=
"days"
, length.
out
=
7
)
(
"This week's dates:"
)
(
week_dates
)
The first day of each month
month_starts
<-
seq
(
as.Date
(
"2026-01-01"
)
,
as.Date
(
"2026-12-01"
)
,
by
=
"months"
)
(
"First day of each month:"
)
(
month_starts
)
The output of the code above is:
[1] "本周日期:"
[1] "2026-05-11" "2026-05-12" "2026-05-13" "2026-05-14"
[5] "2026-05-15" "2026-05-16" "2026-05-17"
[1] "每月第一天:"
[1] "2026-01-01" "2026-02-01" "2026-03-01" "2026-04-01"
[5] "2026-05-01" "2026-06-01" "2026-07-01" "2026-08-01"
[9] "2026-09-01" "2026-10-01" "2026-11-01" "2026-12-01"
R Language Examples