This function takes a vector of Date objects and calculates the week and year according to ISO 8601. It is flexible in its output.
isoweek(x, type ="both_num", sep ="-", inv =FALSE, colnames = c("isoyear","isoweek"))
Arguments
x: A vector of class Date (of length >=1)
type: A string (one of "week", "year", "both_text", "both_num" or "matrix") that determines the kind of output the function returns. See "Return value".
sep: Seperator between year and week, applicable if type="both_text"
inv: If type="both_text", and inv=FALSE, then year comes before week. If inv=TRUE, week comes before year.
colnames: Names for the matrix columns if type="matrix"
Returns
Different according to the function's type argument. If "both_num" (the default), a vector of 6-digit integers is returned, in a YYYYWW format. If "week" or "year", only the week number or year is returned, respectively. If "both_text", then a character vector of the same length as x is returned, containing both the year and week number, seperated by sep, and inverted if inv=TRUE. Finally, if type="matix", both year and week numbers are returned in a two-column matrix, with the columns named as in colnames.
Details
This function calculates the week number according to ISO 8601. Note that dates near the start or end of a given year may belong to the previous or next year respectively, thus the year needs to be calculated too.