【问题标题】:Generate first and last Julian day of each month生成每个月的第一个和最后一个儒略日
【发布时间】:2018-02-02 17:20:08
【问题描述】:

我想为非闰年(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 作为序列,这样前一年就不是闰年。

标签: r as.date


【解决方案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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-23
      • 1970-01-01
      • 2022-11-24
      • 1970-01-01
      • 2012-05-29
      • 1970-01-01
      • 2021-03-22
      相关资源
      最近更新 更多