[R] Convert week value to date

Folkes, Michael Michael.Folkes at dfo-mpo.gc.ca
Thu Aug 18 18:11:53 CEST 2011


Thanks David and Duncan,
Before I posted I was considering the approach of multiplying week by 7,
but I couldn't see a clean way to do it so it'll work for any year, and
get Monday's date correct.
I now realize I could write code to evaluate which of the first 7 days
in the year is a Monday and then I'd know the start of week 1 in each
year, and multiply from there.
Thanks
Michael

-----Original Message-----
From: David Winsemius [mailto:dwinsemius at comcast.net] 
Sent: August 17, 2011 2:38 PM
To: Folkes, Michael
Cc: r-help at r-project.org
Subject: Re: [R] Convert week value to date


On Aug 17, 2011, at 4:52 PM, Folkes, Michael wrote:

> Hello all,
> I'm hoping to convert a decimal value for week of the year back to a  
> date object.
> Eg:
> strptime(paste(2010,1:52,sep=" "),format="%Y %W")

Yeah, agree that seems as though it should have been successful. I  
cannot get any of my invocations using %W to work.

 > strptime(Sys.Date(), "%Y-%m-%d-%W")
[1] NA
 > strptime(Sys.Date(), "%Y-%m-%d")
[1] "2011-08-17"
 > strptime(Sys.Date(), "%Y-%m-%d %W")
[1] NA

Oh well.

seq.POSIXt(as.POSIXlt("2010-01-01"), by="week", length=52)

>
> I expected (hoped?) this would give me the date for Monday of each  
> week.  Instead, it's giving me 52 values of today's date.
>
> Where am I erring?
> Thanks
> Michael
>

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list