[R] Extracting year from a date object

joris meys jorismeys at gmail.com
Tue Oct 6 11:39:51 CEST 2009


Hi all,

this one left me a bit puzzled, as I don't seem to find a function to
perform this easily. I must have overlooked the obvious, so sorry in
advance.

I have a list of dates in numerical format (i.e. 34576), defined as
the number of days that passed since january 1st 1900. So I apply the
function :

> MyDate <-as.Date(34576,origin="1900-01-01")
> MyDate
[1] "1994-09-01"

But then I want to do something like :
MyYear <- a.year.function(MyDate)

MyYear should have the numerical value 1994. Alas, I don't find any
function like that. I know I can take a substring of MyDate and
convert to numeric, or even use the function "seq()" :

> length ( seq ( as.Date("1900-01-01"), MyDate, "years" ) ) + 1900 - 1
[1] 1994

but that seems quite a way around. Can somebody tell me where the
appropriate function is hiding?
Thank you in advance.
Joris




More information about the R-help mailing list