julian day form POSIXt object

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

julian day form POSIXt object

maxbre
hello all,

this is my reproducible example data frame

test<-structure(list(date = structure(c(1262300400, 1262304000, 1262304000,
1262307600, 1262307600, 1262311200, 1262311200, 1262314800, 1262314800,
1262318400, 1262318400, 1262322000, 1262322000, 1262325600, 1262325600,
1262329200, 1262329200, 1262332800, 1262332800, 1262336400, 1262336400,
1262340000, 1262340000, 1262343600, 1262343600, 1262347200, 1262347200,
1262350800, 1262350800, 1262354400, 1262354400, 1262358000, 1262358000,
1262361600, 1262361600, 1262365200, 1262365200, 1262368800, 1262368800,
1262372400, 1262372400, 1262376000, 1262376000, 1262379600, 1262379600,
1262383200, 1262383200, 1262386800), class = c("POSIXct", "POSIXt"
), tzone = "")), .Names = "date", row.names = c(NA, -48L), class = "data.frame")

given that, my objective is to create a new variable accounting for the julian day (i.e. the number of consecutive days elapsed from a fixed origin date)

and these are (among many others) my (most) unsuccessful attempts…

library(chron)
#not working!
test$jday<-julian(test$date, origin=as.POSIXct(test$date))

#not working!
test$jday<-julian(test$date, origin=test$date)

#not working!
test$jday <-julian(as.Date(test$date, "%m"),
                   as.Date(test$date, "%d"),
                   as.Date(test$date, "%Y"),
                   origin=c(month=1, day=0, year=2010))


any help for this?

thanks a lot

max
Reply | Threaded
Open this post in threaded view
|

Re: julian day form POSIXt object

David Winsemius

On Sep 29, 2011, at 10:10 AM, maxbre wrote:

> hello all,
>
> this is my reproducible example data frame
>
> test<-structure(list(date = structure(c(1262300400, 1262304000,  
> 1262304000,
> 1262307600, 1262307600, 1262311200, 1262311200, 1262314800,  
> 1262314800,
> 1262318400, 1262318400, 1262322000, 1262322000, 1262325600,  
> 1262325600,
> 1262329200, 1262329200, 1262332800, 1262332800, 1262336400,  
> 1262336400,
> 1262340000, 1262340000, 1262343600, 1262343600, 1262347200,  
> 1262347200,
> 1262350800, 1262350800, 1262354400, 1262354400, 1262358000,  
> 1262358000,
> 1262361600, 1262361600, 1262365200, 1262365200, 1262368800,  
> 1262368800,
> 1262372400, 1262372400, 1262376000, 1262376000, 1262379600,  
> 1262379600,
> 1262383200, 1262383200, 1262386800), class = c("POSIXct", "POSIXt"
> ), tzone = "")), .Names = "date", row.names = c(NA, -48L), class =
> "data.frame")
>
> given that, my objective is to create a new variable accounting for  
> the
> julian day (i.e. the number of consecutive days elapsed from a fixed  
> origin
> date)
>
> and these are (among many others) my (most) unsuccessful attempts…
>
> library(chron)
> #not working!
> test$jday<-julian(test$date, origin=as.POSIXct(test$date))

I thought the error message was reasonably informative:

Error in julian.POSIXt(test$date, origin = as.POSIXct(test$date)) :
   'origin' must be of length one

And the obvious fix was to just give it one starting date:

 > test$jday<-julian(test$date, origin=as.POSIXct(test$date)[1])
 > test$jday
Time differences in days
  [1] 0.00000000 0.04166667 0.04166667 0.08333333 0.08333333  
0.12500000 0.12500000 0.16666667 0.16666667 0.20833333
[11] 0.20833333 0.25000000 0.25000000 0.29166667 0.29166667 0.33333333  
0.33333333 0.37500000 0.37500000 0.41666667
[21] 0.41666667 0.45833333 0.45833333 0.50000000 0.50000000 0.54166667  
0.54166667 0.58333333 0.58333333 0.62500000
[31] 0.62500000 0.66666667 0.66666667 0.70833333 0.70833333 0.75000000  
0.75000000 0.79166667 0.79166667 0.83333333
[41] 0.83333333 0.87500000 0.87500000 0.91666667 0.91666667 0.95833333  
0.95833333 1.00000000
attr(,"tzone")
[1] ""
attr(,"origin")
[1] "2009-12-31 18:00:00 EST"


>
> #not working!
> test$jday<-julian(test$date, origin=test$date)
>
> #not working!
> test$jday <-julian(as.Date(test$date, "%m"),
>                   as.Date(test$date, "%d"),
>                   as.Date(test$date, "%Y"),
>                   origin=c(month=1, day=0, year=2010))
>
Exactly the same errors.

>
> any help for this?
>

Yes:
Read the error messages for meaning and include them in your postings.

--

David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: julian day form POSIXt object

maxbre
yes, you are perfectly right, sorry for that but for me was not so clear the error message!

....and the third code example I posted (not working either) should be of length one (if I'm not wrong again)
 
but most of all what is not included in my reproducible example and get a bit confused my question is the case when the variable "date" contains dates of many years and the objective is to count for each given year the julian date starting from the first day of the corresponding year

how would you deal with this problem?

thanks a lot for your help

max
Reply | Threaded
Open this post in threaded view
|

Re: julian day form POSIXt object

Jeff Newmiller
On Thu, 29 Sep 2011, maxbre wrote:

> yes, you are perfectly right, sorry for that but for me was not so clear the
> error message!
>
> ....and the third code example I posted (not working either) should be of
> length one (if I'm not wrong again)
>
> but most of all what is not included in my reproducible example and get a
> bit confused my question is the case when the variable "date" contains dates
> of many years and the objective is to count for each given year the julian
> date starting from the first day of the corresponding year

So you aren't really interested in julian day, you are interested in day
of year.  Julian day maps all calendar time to number of days from a
SINGLE epoch (origin) which may differ from one problem to the
next, but not from one value to the next within the problem the way
"beginning of year" changes for each value.

> how would you deal with this problem?

test$doy <- as.POSIXlt(test$date)$yday+1

> thanks a lot for your help
>
> max
>
> --
> View this message in context: http://r.789695.n4.nabble.com/julian-day-form-POSIXt-object-tp3855885p3856151.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.
>

---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<[hidden email]>        Basics: ##.#.       ##.#.  Live Go...
                                       Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: julian day form POSIXt object

Prof Brian Ripley
On Thu, 29 Sep 2011, Jeff Newmiller wrote:

> On Thu, 29 Sep 2011, maxbre wrote:

[With the context excised, something the posting guide expressly asks
not be done.]

>> yes, you are perfectly right, sorry for that but for me was not so
>> clear the error message!

A phrase involving pots and kettles springs to mind.

The answer to the question asked was 'see ?julian'

>> ....and the third code example I posted (not working either) should be of
>> length one (if I'm not wrong again)
>>
>> but most of all what is not included in my reproducible example and
>> get a bit confused my question is the case when the variable "date"
>> contains dates of many years and the objective is to count for each
>> given year the julian date starting from the first day of the
>> corresponding year
>
> So you aren't really interested in julian day, you are interested in day of
> year.  Julian day maps all calendar time to number of days from a SINGLE
> epoch (origin) which may differ from one problem to the next, but not from
> one value to the next within the problem the way "beginning of year" changes
> for each value.
>
>> how would you deal with this problem?
>
> test$doy <- as.POSIXlt(test$date)$yday+1

Perhaps, but as described without the +1.  The Julian day of the
origin is taken to be day 0.

I think the OP wanted what ISO 8601:2004 calls 'ordinal dates' and of
which Wikipedia says

'This system is sometimes incorrectly referred to as "Julian Date",
whereas the astronomical Julian Date is a sequential count of the
number of days since day 0 beginning 1 January 4713 BC Greenwich noon,
Julian proleptic calendar (or noon on ISO date -4713-11-24 which uses
the Gregorian proleptic calendar with a year [0000]).'

>> thanks a lot for your help
>>
>> max

--
Brian D. Ripley,                  [hidden email]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: julian day form POSIXt object

maxbre
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))

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
Reply | Threaded
Open this post in threaded view
|

Re: julian day form POSIXt object

maxbre
sorry, I managed to find the solution at last
(by reading the help but sometime is really difficult!)

Class "POSIXlt" is a named list of vectors representing
yday
0–365: day of the year.


another lesson learned

bye
Reply | Threaded
Open this post in threaded view
|

Re: julian day form POSIXt object

Gabor Grothendieck
In reply to this post by maxbre
On Fri, Sep 30, 2011 at 4:47 AM, maxbre <[hidden email]> 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.
>
> ______________________________________________
> [hidden email] 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.
>



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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: julian day form POSIXt object

David Winsemius
In reply to this post by maxbre

On Sep 30, 2011, at 4:47 AM, maxbre 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))
>
> 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?

POSIXlt objects are represented as lists. as.POSIXlt will convert a  
POSIXct object to that class.  The "yday" element is the number of  
days that have passed since the first day of the year. The "$"  
operator accesses list elements by name.

?DateTimeClasses
?Extract   #  same as ?"["


>
> if someone would be so kind to explain me this part I would be very  
> grateful
>
> a newbie in R
> lesson learned
>

David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] 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.