[R] day of the year for chron objects
Gabor Grothendieck
ggrothendieck at gmail.com
Fri Jun 8 12:54:23 CEST 2012
On Fri, Jun 8, 2012 at 4:01 AM, Agustin Lobo <Agustin.Lobo at ictja.csic.es> wrote:
> Hi!
> Is not there an standard R function to retrieve the day of the year
> (since 1st Jan of the same year)?
> I know I can make my own using julian, but find it weird that having
> days(), months() etc doy() does not exist as an standard function.
>
> Also, is the following not a bit inconsistent?
>
>> a <- chron("20100506",format="ymd")
>> a
> [1] 100506
>> years(a)
> [1] 2010
> Levels: 2010
>
> but instead have to cast to Date to apply julian:
>> julian(a)
> Error in names(d) : 'd' is missing
>> julian(as.Date(a))
> [1] 14735
> attr(,"origin")
> [1] "1970-01-01"
>
First note that ymd does not work without a separator. We get a
number rather than a chron date:
> chron("20100506",format="ymd")
[1] 100506
Instead do it like this:
> a <- as.chron("20100506", format = "%Y%m%d"); a
[1] (05/06/10 00:00:00)
> as.numeric(format(as.Date(a), format = "%j"))
[1] 126
If there are no times involved it might be simpler just to use Date in
the first place:
> d <- as.Date("20100506", format = "%Y%m%d"); d
[1] "2010-05-06"
> as.numeric(format(d, format = "%j"))
[1] 126
See ?strptime
--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com
More information about the R-help
mailing list