# [R] julian day form POSIXt object

Gabor Grothendieck ggrothendieck at gmail.com
Fri Sep 30 13:22:11 CEST 2011

```On Fri, Sep 30, 2011 at 4:47 AM, maxbre <mbressan at arpa.veneto.it> wrote:
> here is an alternative long and winded solution to the problem sticking on
> the julian function of chron package
>
> test\$year <- as.integer(as.character(test\$date, "%Y"))
> test\$month <- as.integer(as.character(test\$date, "%m"))
> test\$day <- as.integer(as.character(test\$date, "%d"))
> test\$hour <- as.integer(as.character(test\$date, "%H"))
>
> test\$jday <-julian(test\$month,test\$day,test\$year,origin=c(month=1, day=0,
> year=test\$year))

Assume:

library(chron)
## test data
d <- as.chron(c("2000-01-01", "2000-02-01"))

## Then here are a few more approaches:

## 1
with(month.day.year(d), julian(month, day, year, origin = c(1, 0, year)))

## 2 - yearmon represents a year and month as a year + month/12 where
## month is 0 for jan, 1 for feb, etc.
library(zoo)
as.numeric(d - as.chron(trunc(as.yearmon(d))) + 1)

## 3
as.numeric(format(as.Date(d), "%j"))

Or if d is one or more "Date" class dates then #1 still works.  #2
works if we replace as.chron with as.Date.  #3 can be reduced to
as.numeric(format(d, "%j")) and does not require any external
packages.

or replace as.chron with as.Date if d is one or more "Date" class dates.

>
> thank you all for helping me in sorting out the problem
>
> by the way I like very much this very compact solution
> test\$doy <- as.POSIXlt(test\$date)\$yday+1
> but on the other hand I'm still struggling to fully understand it,
> especially for what is concerning the part after \$: so what is \$yday for?
>
> if someone would be so kind to explain me this part I would be very grateful
>
> a newbie in R
> lesson learned
>
> max
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/julian-day-form-POSIXt-object-tp3855885p3859187.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> and provide commented, minimal, self-contained, reproducible code.
>

--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

```