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

时间:2018-02-02 17:20:09

标签: r as.date

我想创建一个非闰年(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)

但我怎样才能找到每年的第一个和最后一个朱利安日?

2 个答案:

答案 0 :(得分:1)

调用每月第一次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

答案 1 :(得分:0)

我使用了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)