[R] day of the year for chron objects

Agustin Lobo Agustin.Lobo at ictja.csic.es
Fri Jun 8 13:55:11 CEST 2012


Thanks Gabor,
The trick to solve my problem is the %j, many thanks. I've seen now
it's documented in the help page of strptime as you indicate.

But please note that using format="ymd" I do get a chron object

and note that the value is the date without the 2 first digits

> require(chron)
Loading required package: chron
> a <- chron("20100506",format="ymd")
> class(a)
[1] "dates" "times"
> a
[1] 100506

Also, the use of "ymd" is documented in the help page of chron():
"The dates format can be any permutation of the characters "d", "m",
or "y" delimited by a separator (possibly null), e.g., "m/d/y",
"d-m-y", "ymd", are all valid; "


Thanks!

Agus

On Fri, Jun 8, 2012 at 12:54 PM, Gabor Grothendieck
<ggrothendieck at gmail.com> wrote:
> 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



-- 
--
Dr. Agustin Lobo
Institut de Ciencies de la Terra "Jaume Almera" (CSIC)
Lluis Sole Sabaris s/n
08028 Barcelona
Spain
Tel. 34 934095410
Fax. 34 934110012
e-mail Agustin.Lobo at ictja.csic.es
https://sites.google.com/site/aloboaleu/



More information about the R-help mailing list