0

I want to convert dates into Julian day

x <- c('5/5/2007')
# convert to day of year (Julian date) 

strptime(x, "%m/%d/%Y")$yday+1 
#125

There are two things I do not understand in this:

1) What is y$day + 1 doing?

When I run it for more dates, it gives me NA for some dates.

date<-c("09/06/1997","01/06/1998","26/05/1999","02/06/2000","01/06/2001","06/06/2002","05/06/2003",
    "21/05/2004","16/06/2005","27/05/2006","08/06/2007","09/06/1997","01/06/1998")

strptime(date, "%m/%d/%Y")$yday+1 
[1] 249   6  NA  37   6 157 126  NA  NA  NA 218 249   6

I am not sure why is this happening? And why do we need $yday+1

user53020
  • 889
  • 2
  • 10
  • 33
  • The first is incrementing by one day (+1). You are getting NAs because you have a typo in your format. It should be : "%d/%m/%Y". – lmo Jul 06 '16 at 13:24
  • Some of the dates in your vector are in the format `%d/%m/%Y`, and hence the NAs in your result – Sumedh Jul 06 '16 at 13:24
  • For more about Julian days: http://stackoverflow.com/questions/20080241/how-to-convert-number-to-julian-date-in-r http://stackoverflow.com/questions/21414847/convert-a-date-vector-into-julian-day-in-r – Sumedh Jul 06 '16 at 13:25
  • Thanks. I understand this now. – user53020 Jul 06 '16 at 13:27
  • I'd recommend to either move the comments that answered the question into an answer or to remove the question – Hack-R Jul 06 '16 at 13:50

0 Answers0