[R] years from as.POSIXlt

Gabor Grothendieck ggrothendieck at myway.com
Tue Mar 9 02:14:08 CET 2004



days and years are from the chron package and operate on
chron objects, not POSIXlt objects.

If you have a POSIXlt object, EDATE, you can get the years 
and julian day of the year like this:

as.numeric(format(EDATE,"%Y"))
as.numeric(format(EDATE,"%j"))

See ?strptime for those % codes and many others.

Alternately you can convert your dates to chron objects
instead of POSIXlt objects and then you can use years and 
days as you did below.  See the chron package for that.

---

Date:   Mon, 8 Mar 2004 15:50:01 +0100 
From:   Christian Schulz <ozric at web.de>
To:   <r-help at stat.math.ethz.ch> 
Subject:   [R] years from as.POSIXlt  

 
Hi,

how it's possible to extract the year and the number
of days from Julian date. i'm little confused about the last two
functions and ?years .

EDATE comes from sqlQuery with as.is=T
EDATE <- as.POSIXlt(datvears$ENROLLDAY)

Many thanks, Christian


> EDATE[1:5] 
[1] "2000-06-30 11:25:01" "2000-06-30 11:39:55" "2000-06-30 12:11:11"
[4] "2000-06-30 12:13:32" "2000-06-30 12:50:12"
> weekdays(EDATE[1:5])
[1] "Freitag" "Freitag" "Freitag" "Freitag" "Freitag"
> months(EDATE[1:5])
[1] "Juni" "Juni" "Juni" "Juni" "Juni"
> years(EDATE[1:5])
NULL
> days(EDATE[1:5])
NULL
>




More information about the R-help mailing list