Convert to day of a year in R

In R often time we need to find the exact nth day of a date in that particular year.

The snippet below will be very helpful for this:

x <- c(‘5/5/2007′,’12/31/2007′,’1/2/2008’)

#Convert to day of year (julian date) — 

strptime(x,”%m/%d/%Y”)$yday+1

The result will be as follows:

[1] 125 365 2

As you can notice in the above result, 5/5/2007 refers to 125th day of the year 2007. Similary, 12/31/2007 refers to 365th day of year 2007 and 1/2/2008 refers to 2nd day of 2008. So, this small snippet is very helpful if you want to create a vector of nth day of the date.

Good luck

Advertisements
Aside | This entry was posted in Snippets and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s