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 ? Thanks Shyam [[alternative HTML version deleted]]
Date (R 1.9.0 only), chron (in package chron) and POSIXct classes all support all those operations. For example,> dat.char <- c("1-Jan-01", "7-Jan-01", "14-Jan-01", "21-Feb-01")> # chron > require(chron)[1] TRUE> dat <- chron(dat.char, format="day-month-year") > min(dat);max(dat);sort(dat);order(dat)[1] 01-January-2001 [1] 21-February-2001 [1] 01-January-2001 07-January-2001 14-January-2001 21-February-2001 [1] 1 2 3 4> # POSIXct > dat <- as.POSIXct(strptime(dat.char,format="%d-%b-%y")) > dat[1] "2001-01-01 Eastern Standard Time" "2001-01-07 Eastern Standard Time" [3] "2001-01-14 Eastern Standard Time" "2001-02-21 Eastern Standard Time"> min(dat);max(dat);sort(dat);order(dat)[1] "2001-01-01 Eastern Standard Time" [1] "2001-02-21 Eastern Standard Time" [1] "2001-01-01 Eastern Standard Time" "2001-01-07 Eastern Standard Time" [3] "2001-01-14 Eastern Standard Time" "2001-02-21 Eastern Standard Time" [1] 1 2 3 4> # Date > R.version.string[1] "R version 1.9.0, 2004-03-05"> dat <- as.Date(dat.char,format="%d-%b-%y") > min(dat);max(dat);sort(dat);order(dat)[1] "2001-01-01" [1] "2001-02-21" [1] "2001-01-01" "2001-01-07" "2001-01-14" "2001-02-21" [1] 1 2 3 4>Date: Mon, 22 Mar 2004 23:13:03 +0530 From: Shyam Sundaram <shyamss at hotmail.com> To: <r-help at stat.math.ethz.ch> Subject: [R] Date operations 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 ?
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