[R] Help on Dates in R again

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Sep 21 09:13:09 CEST 2006


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,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list