[R] Date operations

Gabor Grothendieck ggrothendieck at myway.com
Mon Mar 22 19:23:01 CET 2004



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 ?




More information about the R-help mailing list