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") 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 _______________________________________________________ Michael Folkes Salmon Stock Assessment Canadian Dept. of Fisheries & Oceans Pacific Biological Station 3190 Hammond Bay Rd. Nanaimo, B.C., Canada V9T-6N7 Ph (250) 756-7264 Fax (250) 756-7053 Michael.Folkes@dfo-mpo.gc.ca [[alternative HTML version deleted]]
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
Hi Michael I am not sure of your decimal format of the week but if you can convert it to a calendar day of the year by multiplying by 7 then you could you could use eg if 321 was the calendar day strptime(paste(2010, 321,sep=" "),format="%Y %j") [1] "2010-11-17" Regards Duncan Duncan Mackay Department of Agronomy and Soil Science University of New England ARMIDALE NSW 2351 Email: home mackay at northnet.com.au At 06:52 18/08/2011, you 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") > >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 > >_______________________________________________________ >Michael Folkes >Salmon Stock Assessment >Canadian Dept. of Fisheries & Oceans >Pacific Biological Station >3190 Hammond Bay Rd. >Nanaimo, B.C., Canada >V9T-6N7 >Ph (250) 756-7264 Fax (250) 756-7053 Michael.Folkes at dfo-mpo.gc.ca > > > [[alternative HTML version deleted]] > >______________________________________________ >R-help at r-project.org mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code.
On Wed, Aug 17, 2011 at 4:52 PM, Folkes, Michael <Michael.Folkes at dfo-mpo.gc.ca> 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") > > 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. >If the objective is to get all the Mondays in 2011 then: s <- seq(as.Date("2011-01-01"), as.Date("2011-12-31"), by = "day") s[format(s, "%w") == "1"] If the objective is to get the previous Monday relative to an input date (or the same date if its already Monday) then in the zoo-quickref vignette is a function nextfri which outputs the next Friday on or after the input date. We can readily modify that to get the prior Monday on or before the current date: prevmonday <- function(x) 7 * floor(as.numeric(x-1+4) / 7) + as.Date(1-4) For example,> prevmonday(Sys.Date())[1] "2011-08-15"> prevmonday(prevmonday(Sys.Date()))[1] "2011-08-15" -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
Possibly Parallel Threads
- lattice: passing multiple lty values to the key/legend
- Floating points and floor() ?
- maptools::sunriset() daylight savings to stardard time change
- RODBC excel - need to preserve (or extract) numeric column names
- Hmisc binconf function value interpretation during narrow confidence intervals