day_of_year {cropgrowdays}R Documentation

Calculate day of year from a date

Description

day_of_year returns the day of the year as an integer. The first day of the year is 1 January for the calendar year, 1 July for the Australian financial year or can be specified as any day of the year if desired.

Usage

day_of_year(
  x,
  type = c("calendar", "financial", "other"),
  return_year = FALSE,
  base = NULL
)

Arguments

x

A date used for calculation the day of the year.

type

A character string specifying the type of year. “calendar” is a calendar year starting on 1 January, “financial” an Australian financial year beginning on 1 July and “other” is for a year starting on another date which is specified in base. Default: “calendar”

return_year

A logical indicating whether to return the year or not. Default: FALSE

base

A list specifying the day and month of the first day to be used in calculations. The name of the day component must be named either “day” or “d”, and the month must be named either “month” or “m”.

Value

A numeric vector containing day of the year. If return_year is TRUE then a data.frame is returned containing two columns day and year. The first column day is always numeric but the class of the year column depends on type. For type “calendar”, “financial”, “other” then year is numeric, character and the base date as a Date, respectively.

See Also

date_from_day_year for converting day of year and year to a Date and day_of_harvest for calculating the day of harvest given a sowing date where the start of the year is the first day of the year which contains the sowing date

Examples

library(lubridate)
## Day of Calendar Year
day_of_year(ymd("2020-01-05"))
day_of_year(ymd("2021-01-05"))
day_of_year(ymd(c("2020-06-05"), "2021-06-05")) # 29 Feb in 2020 only
day_of_year(ymd("2020-12-31"))
day_of_year(ymd("2021-12-31"))
day_of_year(ymd("2020-12-31"), return_year = TRUE)
day_of_year(ymd(c("2020-12-31", "2020-07-01", "2020-01-01")))
day_of_year(ymd(c("2020-12-31", "2020-07-01", "2020-01-01")),
            return_year = TRUE)

## Day of Financial Year
day_of_year(ymd(c("2020-12-31", "2020-07-01", "2020-01-01")),
            type = "financial")
day_of_year(ymd(c("2020-12-31", "2020-07-01", "2020-01-01")),
            type = "financial", return_year = TRUE)
day_of_year(x = ymd("2021-09-05"), type = "financial") # 67

## Specify the year starts on 1 September
day_of_year(ymd(c("2020-12-31", "2020-07-01", "2020-01-01")),
            type = "other", base = list(d = 1, m = 9))
day_of_year(ymd(c("2020-12-31", "2020-07-01", "2020-01-01")),
            type = "other", base = list(d = 1, m = 9), return_year = TRUE)


[Package cropgrowdays version 0.2.1 Index]