Convert Julian day date vector to R

I have a date column in the format: 16Jun10 and I would like to extract Julian day. I have different years.

I tried the julian and mdy.date functions and it doesn't seem to work.

+6
source share
4 answers

Try converting from the character class (e.g. text) to the POSIXlt class, and then extract the Julian day ( yday ):

 tmp <- as.POSIXlt("16Jun10", format = "%d%b%y") tmp$yday # [1] 166 

More about function settings:

 ?POSIXlt ?DateTimeClasses 

Another option is to use the Date class and then use format to retrieve the Julian day (note that this class defines julian days between 1: 366, and POSIXlt is 0: 365):

 tmp <- as.Date("16Jun10", format = "%d%b%y") format(tmp, "%j") # [1] "167" 
+17
source

Similarly:

 require(lubridate) x = as.Date('2010-06-10') yday(x) [1] 161 

Also note using lubridate:

 > dmy('16Jun10') [1] "2010-06-16 UTC" 
+15
source

You can use the R insol package, which has a JD(x, inverse=FALSE) function that converts POSIXct to Julian Day (JDN).

Package

insol also has JDymd(year,month,day,hour=12,minute=0,sec=0) for custom dates.

To display the entire Julian date (JD), you may need to set options(digits=16) .

+3
source
 my.data = read.table(text = " OBS MONTH1 DAY1 YEAR1 1 3 1 2012 2 3 31 2012 3 4 1 2012 4 4 30 2012 5 5 1 2012 6 5 31 2012 7 6 1 2012 8 6 30 2012 9 7 1 2012 10 7 31 2012 ", header = TRUE, stringsAsFactors = FALSE) my.data$MY.DATE1 <- do.call(paste, list(my.data$MONTH1, my.data$DAY1, my.data$YEAR1)) my.data$MY.DATE1 <- as.Date(my.data$MY.DATE1, format=c("%m %d %Y")) my.data$my.julian.date <- as.numeric(format(my.data$MY.DATE1, "%j")) my.data 

Returns, which is technically incorrect, since Julian dates do not return to 1 on the first day of every January:

http://en.wikipedia.org/wiki/Julian_day

Below are the dates:

  OBS MONTH1 DAY1 YEAR1 MY.DATE1 my.julian.date 1 1 3 1 2012 2012-03-01 61 2 2 3 31 2012 2012-03-31 91 3 3 4 1 2012 2012-04-01 92 4 4 4 30 2012 2012-04-30 121 5 5 5 1 2012 2012-05-01 122 6 6 5 31 2012 2012-05-31 152 7 7 6 1 2012 2012-06-01 153 8 8 6 30 2012 2012-06-30 182 9 9 7 1 2012 2012-07-01 183 10 10 7 31 2012 2012-07-31 213 
+2
source

All Articles