[R] date from weeknumber
Gabor Grothendieck
ggrothendieck at gmail.com
Wed Jul 21 06:37:06 CEST 2010
On Tue, Jul 20, 2010 at 6:37 PM, H Rao <hydsdrao at gmail.com> wrote:
> Hi,
>
> Is there a function to get the last(or first) day of the week, given the
> week number of the year?
>
> For eg, week number for 7/20 is 29 as obtained by format(Sys.Date(),"%U"),
> is there a function which returns 7/25 - the last day of week # 29
>
%U weeks end on Saturday, not Sunday, so week 29 ends on July 24th
rather than July 25th.
At any rate supposing that we want the first and last day of the week
according to %U and assume that if the week starts or ends in the
following year that we we want the first or last day of this year.
First, define the input year and week.
y <- 2010; w <- 29
# Then create a sequence of days for at least the entire year
# and pick off the range of dates whose format indicates
# the desired year and week
days <- as.Date(paste(y, 1, 1, sep = "-")) + 0:365
range(days[sprintf("%d %02d", y, w) == format(days, "%Y %U")])
# "2010-07-18" "2010-07-24"
More information about the R-help
mailing list