R seq.Date() function - Generating Date Sequences
The R seq.Date() function is used to generate date sequences at equal intervals.
In time series analysis and periodic report generation, creating date sequences is a very common requirement.
The syntax format of the seq.Date() function is as follows:
seq(from, to, by = "days")
Parameter Description:
fromStart date.
toEnd date.
byInterval: "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)
print("This week's dates:")
print(week_dates)
# The first day of each month
month_starts <- seq(as.Date("2026-01-01"),
as.Date("2026-12-01"),
by = "months")
print("First day of each month:")
print(month_starts)
start <- as.Date("2026-05-11")
week_dates <- seq(start, by = "days", length.out = 7)
print("This week's dates:")
print(week_dates)
# The first day of each month
month_starts <- seq(as.Date("2026-01-01"),
as.Date("2026-12-01"),
by = "months")
print("First day of each month:")
print(month_starts)
After executing the above code, the output 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"Other Extensions
R Language Examples