18

I have a list of dates like these:

library(lubridate)
my.dates <- c("03-01-2006", "28-01-2006", "12-01-2008", "02-02-2006", "03-03-2008")
my.dates <- dmy(my.dates)

I need to extract, the day number of the year from each date as below, where 1st January is day 1:

day.number <- c(3, 28, 12, 33, 62)

I've had a good hunt around google and SO but cannot find an easy way of doing this. Help appreciated, thanks.

luciano
  • 13,158
  • 36
  • 90
  • 130
  • @DidzisElferts That is *not* a duplicate - the key issue here is the **lubridate** representation of the dates, which is significantly different from the `POSIXt`-based solutions in the duplicate you propose. I have voted to leave open in the review queue. – Gavin Simpson Mar 31 '13 at 16:20

2 Answers2

23

you can use yday function from lubridate since you're already using it:

> yday(my.dates)
# [1]  3 28 12 33 63
Arun
  • 116,683
  • 26
  • 284
  • 387
  • Thanks. Apologies for missing question already asked. I'm using `lubridate` so will stick with that package – luciano Mar 16 '13 at 19:57
19

Try POSIXlt and its attributes(my.dates)

my.dates = as.POSIXlt(my.dates,format="%d-%m-%Y")
my.dates$mday
[1]  3 28 12  2  3
my.dates$yday
[1]  2 27 11 32 62
statquant
  • 13,672
  • 21
  • 91
  • 162