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.
So, as we have the date from the year 2022, the date function returns 31-Dec-2021. When you subtract 31-Dec-2021 from the 02-Apr-2022 you get 92 in the result which is the day number of 02-Apr-2022 in the year 2022.
Excel YEAR function=YEAR(A2) - returns the year of a date in cell A2. =YEAR("20-May-2015") - returns the year of the specified date.
To find the number of days between these two dates, you can enter “=B2-B1” (without the quotes into cell B3). Once you hit enter, Excel will automatically calculate the number of days between the two dates entered. Note that Excel recognizes leap years.
you can use yday
function from lubridate
since you're already using it:
> yday(my.dates)
# [1] 3 28 12 33 63
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With