How to convert a date to the position of in a year (day of the year)?

420 views Asked by At

I would like to include seasonality analysis in my model using a*sin(x)+b*cos(x), as where x is the day of the year.

For example "1990-1-1" retures 1; or like "1990-1-10" retures 10.

Also, since my data is in hourly base, what I was planning to do is to find out of the day of the year then times it by 24 then plus the striped out "hour number" to represents its order within a year. Could you also let me know if there are any better ways to do that.

1

There are 1 answers

0
thelatemail On BEST ANSWER

Format your dates and you're done:

x <- as.Date(c("1990-1-1","1990-1-10","2010-06-16"))
as.numeric(format(x,"%j"))
#[1]  1 10 167