[R] julian day form POSIXt object

David Winsemius dwinsemius at comcast.net
Thu Sep 29 16:50:34 CEST 2011


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



More information about the R-help mailing list