生成每个月的第一个和最后一个儒略日

Generate first and last Julian day of each month

我想为非闰年(365 天)创建一个包含每个月开始和结束日期的向量 在儒略日。

类似

start.month <- c(1, 32, 60, .....)
end.month <- c(31, 59, 90, .....)

我如何在 R 中执行此操作?

我只能用这个生成一年中的月份:

seq(as.Date("2001/1/1"), by = "month", length.out = 12)

但是如何找到每年的第一个和最后一个儒略日呢?

将您的日期顺序称为每个月的第一天starts。然后定义ends = starts - 1。那么儒略日是format(c(starts, ends), "%j")。使用 2002 作为序列,这样前一年就不是闰年。

    starts = seq(as.Date("2002/1/1"), by = "month", length.out = 12)
    ends = starts - 1
    x = as.numeric(format(c(starts, ends), "%j"))
    sort(x)
    #  [1]   1  31  32  59  60  90  91 120 121 151 152 181 182 212 213 243 244 273 274 304 305
    # [22] 334 335 365

我会使用 lubridate

library(lubridate)
dates <- seq(as.Date("2001/1/1"), by = "month", length.out = 12)
end_dates <- dates
day(end_dates) <- days_in_month(end_dates)

start_month <- yday(dates)
end_month <- yday(end_dates)

start_month
[1]   1  32  60  91 121 152 182 213 244 274 305 335
end_month
[1]  31  59  90 120 151 181 212 243 273 304 334 365

或者如果您知道这不是闰年,您可以使用

end_month <- c(1 + start_month[-1], 365)