Re: [R] Help on Dates in R again

From: Prof Brian Ripley <>
Date: Thu 21 Sep 2006 - 07:13:09 GMT

On Thu, 21 Sep 2006, Thorsten Muehge wrote:

> Hello R Experts,
> I want to aggregate parameters by week. But our production week ends Friday
> night instead of Sunday Night which is the default value in R.

The default in ISO8601, not just in R, but that is %W, not %U as used below.

> In order to solve the problem I want to substract two days from the current
> data and than use the R function
> test$week<-format(test$dates,"%U");
> with a test&dates format equal to "2006-09-21".
> How do I substract the two days from the test$dates column in the
> data.frame?

You have not told us what class test$dates is! Assuming it is "Date", test$dates-2.

*However*, to do what you ask, you need to add 1:

> dates <- seq(as.Date("2006-09-21"), by=1, len=7)
> format(dates+1, "%U")

[1] "38" "38" "39" "39" "39" "39" "39"

There is a potential problem here at year ends (there is anyway in the ISO8601 definition). Another way is just

(unclass(dates) - 2) %/% 7

which orders weeks across years.

Brian D. Ripley,        
Professor of Applied Statistics,
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________ mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.
Received on Thu Sep 21 17:17:54 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Thu 21 Sep 2006 - 07:30:09 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.