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" Thanks Agus -- -- 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/
Hi Agustin, You can check and adapt this solution to your needs: http://stackoverflow.com/questions/9465817/count-days-per-year Regards, Carlos Ortega www.qualityexcellence.es 2012/6/8 Agustin Lobo <Agustin.Lobo@ictja.csic.es>> 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" > > Thanks > > Agus > > -- > -- > 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@ictja.csic.es > https://sites.google.com/site/aloboaleu/ > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Saludos, Carlos Ortega www.qualityexcellence.es [[alternative HTML version deleted]]
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
The following appears to be pretty "standard" (though not included in chron) to me: b = ISOdate(2001,12,12) format(b, '%j') or is that what you refer to with "I know I can make my own using julian" ? No idea about the other questions though ... Jannis On 08.06.2012 10:01, Agustin Lobo 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" > > Thanks > > Agus >