[R] Odp: Extracting year from a date object

Petr PIKAL petr.pikal at precheza.cz
Tue Oct 6 11:54:29 CEST 2009


r-help-bounces at r-project.org napsal dne 06.10.2009 11:39:51:

> 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()" :
> 

E.g.

 as.numeric(format(MyDate, "%Y"))
[1] 1994

Regards
Petr


> > 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
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list