Friday, June 3, 2011

R - convert date into day of year

strptime contains a lot of functionality to converting a date (type ?strptime for details).

For converting a Y-m-d given date into a day-of-year number use the following syntax:

strptime(c("2011-03-04","2010-01-01"), "%Y-%m-%d")$yday+1

(you have to add 1 because the counting starts at 0)

if your date is given in a different format change the second argument within the brackets

e.g. for "01.10.1999":
strptime("01.10.1999, "%d.%m.%Y")$yday+1

more on dates with the lubridate package
more on dates with the format command

No comments :

Post a Comment