[R] Date operations

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Mar 22 19:40:37 CET 2004


All of min, max and sort work for R's dates.  See ?DateTimeClasses.  You 
appear to have character strings, so just need to convert them.

R 1.9.0 will have classes for dates as well as date-times.

For example (1.9.0)

x <- c("1-Jan-01", "7-Jan-01", "14-Jan-01", "21-Feb-01")
y <- as.Date(x, format="%d-%b-%y")
min(y); max(y); sort(y)

On Mon, 22 Mar 2004, Shyam Sundaram wrote:

> Hello:
> Thanks in advance for your time ?
> 
> I am having a data.frame with one of the columns containing the weeks as
> follows. How can I do the following in the most efficient way ?
> 
> 1. Find the minimum date ?
> 2. Find the maximum date ?
> 3. How do we sort based on ascending order the date ?
> 
> An example as follows.
> 
> Week
> 1-Jan-01 (<----- MIN DATE)
> 7-Jan-01
> 14-Jan-01
> 21-Feb-01 (<----- MAX DATE)
> 
> What is a good way to do these most effectively in R ?

-- 
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