0

I want to creat a vector of start and end day of each month for a non-leap year (365 days) in julian days.

Something like

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

How do I do this in R?

I can only generate the months of a year using this:

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

But how do I find the first and last Julian day for each year?

Jaap
  • 81,064
  • 34
  • 182
  • 193
89_Simple
  • 3,393
  • 3
  • 39
  • 94
  • Call your date sequence of the firsts of each month `starts`. Then define `ends = starts - 1`. Then the julian days are `format(c(starts, ends), "%j")`. Use 2002 for the sequence so that the previous year isn't a leap year. – Gregor Thomas Feb 02 '18 at 17:25

2 Answers2

2

Call your date sequence of the firsts of each month starts. Then define ends = starts - 1. Then the julian days are format(c(starts, ends), "%j"). Use 2002 for the sequence so that the previous year isn't a leap year.

    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
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
0

I'd use 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

or if you know it's not a leapyear, you could use

end_month <- c(1 + start_month[-1], 365)
Russ Hyde
  • 2,154
  • 12
  • 21