Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a date vector into Julian day in R

Tags:

I have a column of dates in the format: 16Jun10 and I would like to extract the Julian day. I have various years.

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

like image 565
user3166363 Avatar asked Jan 28 '14 19:01

user3166363


People also ask

How do you make a Julian date in R?

You can use R's insol package which has a JD(x, inverse=FALSE) function which converts POSIXct to Julian Day Number (JDN). insol package also has JDymd(year,month,day,hour=12,minute=0,sec=0) for custom dates.

How do you format a Julian date?

A Julian date is sometimes used to refer to a date format that is a combination of the current year and the number of days since the beginning of the year. For example, January 1, 2007 is represented as 2007001 and December 31, 2007 is represented as 2007365.

What is Julian day formula?

Multiply the number of non-leap years by 365, and the number of leap years by 366. Add the two totals together for a total number of days in all years.


1 Answers

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

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

For more details on function settings:

?POSIXlt ?DateTimeClasses 

Another option is to use a Date class, and then use format to extract a julian day (notice that this class define julian days between 1:366, while POSIXlt is 0:365):

tmp <- as.Date("16Jun10", format = "%d%b%y") format(tmp, "%j") # [1] "167" 
like image 199
Marc in the box Avatar answered Oct 04 '22 11:10

Marc in the box