Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert ordinal date day-month-year format using R

Tags:

date

r

I have log files where the date is mentioned in the ordinal date format.

wikipedia page for ordinal date

i.e 14273 implies 273'rd day of 2014 so 14273 is 30-Sep-2014.

is there a function in R to convert ordinal date (14273) to (30-Sep-2014).

Tried the date package but didn come across a function that would do this.

like image 436
vinay Avatar asked Sep 06 '25 22:09

vinay


1 Answers

Try as.Date with the indicated format:

as.Date(sprintf("%05d", 14273), format = "%y%j")
## [1] "2014-09-30"

Notes

  1. For more information see ?strptime [link]

  2. The 273 part is sometimes referred to as the day of the year (as opposed to the day of the month) or the day number or the julian day relative to the beginning of the year.

  3. If the input were a character string of the form yyjjj (rather than numeric) then as.Date(x, format = "%y%j") will do.

Update Have updated to also handle years with one digit as per comments.

like image 174
G. Grothendieck Avatar answered Sep 08 '25 12:09

G. Grothendieck