Re: [R] Help on Dates in R again

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
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,                  ripley@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

______________________________________________
R-help@stat.math.ethz.ch 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.
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 https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.