Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extract day number of year from dates

Tags:

date

r

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.

like image 916
luciano Avatar asked Mar 16 '13 19:03

luciano


People also ask

How do you find the day number of the year?

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.

How do you extract a year from a date in Excel?

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.

How do I get the day number in Excel?

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.


2 Answers

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

> yday(my.dates)
# [1]  3 28 12 33 63
like image 111
Arun Avatar answered Oct 12 '22 01:10

Arun


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
like image 25
statquant Avatar answered Oct 12 '22 02:10

statquant