The epiweek function takes a scalar x or vector x and returns the numeric epiweek for that date.
x can be entered as in any format recognized by lubridate's isoweek i.e., a date-time object. Must be a POSIXct, POSIXlt, Date, chron, yearmon, yearqtr, zoo, zooreg, timeDate, xts, its, ti, jul, timeSeries, or fts object
Here is the "definition" I've been working from (this defines both week and year).
The first epi week of the year ends, by definition, on the first Saturday of January, as long as it falls at least four days into the month. Each epi week begins on a Sunday and ends on a Saturday.
This can be achieved by simple change to isoweek and isoyear.
epiweek is based on isoweek from lubridate. The difference is in dn, where we add +6 here and +4 in isoweek because here we want to know where first Sat falls rather than first Thurs as in isoweek.
epiweek <- function(x)
{
require(lubridate)
xdate <- make_datetime(year(x), month(x), day(x))
dn <- 1 + (wday(xdate) + 6)%%7
nth <- xdate + ddays(4 - dn)
jan1 <- make_datetime(year(nth), 1, 1)
1L + as.integer(difftime(nth, jan1, units = "days")) %/% 7L
}
epiyear <- function(x)
{
require(lubridate)
xdate <- make_datetime(year(x), month(x), day(x))
dn <- 1 + (wday(xdate) + 6)%%7
nth <- xdate + ddays(4 - dn)
year(nth)
}
The epiweek function takes a scalar x or vector x and returns the numeric epiweek for that date.
x can be entered as in any format recognized by lubridate's isoweek i.e., a date-time object. Must be a POSIXct, POSIXlt, Date, chron, yearmon, yearqtr, zoo, zooreg, timeDate, xts, its, ti, jul, timeSeries, or fts object
Here is the "definition" I've been working from (this defines both week and year).
The first epi week of the year ends, by definition, on the first Saturday of January, as long as it falls at least four days into the month. Each epi week begins on a Sunday and ends on a Saturday.
This can be achieved by simple change to isoweek and isoyear.
epiweek is based on isoweek from lubridate. The difference is in dn, where we add +6 here and +4 in isoweek because here we want to know where first Sat falls rather than first Thurs as in isoweek.
epiweek <- function(x)
{
require(lubridate)
xdate <- make_datetime(year(x), month(x), day(x))
dn <- 1 + (wday(xdate) + 6)%%7
nth <- xdate + ddays(4 - dn)
jan1 <- make_datetime(year(nth), 1, 1)
1L + as.integer(difftime(nth, jan1, units = "days")) %/% 7L
}
epiyear <- function(x)
{
require(lubridate)
xdate <- make_datetime(year(x), month(x), day(x))
dn <- 1 + (wday(xdate) + 6)%%7
nth <- xdate + ddays(4 - dn)
year(nth)
}