Convert a date vector into Julian day in R

user3166363 picture user3166363 · Jan 28, 2014 · Viewed 39.9k times · Source

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.

Answer

Marc in the box picture Marc in the box · Jan 28, 2014

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"