Several previous posts have asked how to get Julian dates in R. One post, in particular, really needed ordinal dates, and I sent an answer:
Convert Julian day date vector to R
I indicated that my answer did indeed give ordinal dates, not Julian dates. Today I tried to modify my answer to show how to get the true Julian dates, and I got confused when using the package R chron.
Below is the code Rthat generates a set of random dates and then tries to get the corresponding Julian dates. I initially tried using the package R chronto create Julian dates and the US Naval Observatory website to check my answers:
http://aa.usno.navy.mil/data/docs/JulianDate.php
My answers did not match.
In the end, I was able to get Julian dates that match dates from the US Naval Observatory (after rounding the decimal point) using the equation from Wikipedia:
http://en.wikipedia.org/wiki/Julian_day
My question is: what am I doing wrong in chron? How do I get Julian dates in chronthat match the data obtained from the US Naval Observatory, or that match those obtained using the equation from Wikipedia?
Thanks for any advice.
Here is my R code:
set.seed(1234)
n.dates <- 10
day <- sample( 25, n.dates, replace = TRUE)
month <- sample( 11, n.dates, replace = TRUE)
year <- sample(c(-4714:-1, 1:2014), n.dates, replace = TRUE)
year.b <- ifelse(year < 1, year+1, year)
a <- floor((14 - month) / 12)
y <- year.b + 4800 - a
m <- month + 12 * a - 3
julian1 <- day + floor((153*m + 2)/5) + 365*y + floor(y/4) - floor(y/100) + floor(y/400) - 32045
julian2 <- day + floor((153*m + 2)/5) + 365*y + floor(y/4) - 32083
my.data <- data.frame(month, day, year, year.b, a, y, m, julian1, julian2, stringsAsFactors = F)
library(chron)
options(chron.origin = c(month=1, day=1, year= -4713))
my.data$my.julian <- julian(my.data$month, my.data$day, my.data$year)
my.data