繁体   English   中英

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

[英]Generate first and last Julian day of each month

我想在儒略日中创建一个非ap年(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,以便上一年不是a年。

    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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM