[R] Year and Month extraction from Date object.

Gavin Simpson gavin.simpson at ucl.ac.uk
Thu Oct 29 14:56:39 CET 2009


On Tue, 2009-10-27 at 14:49 -0700, rkevinburton at charter.net wrote:
> Hello,
> 
> I have seen much discussion on Date. But I can't seem to do this
> simple operation. I can convert a string to a date:
> 
> d <- as.Date(DATE, format="%m/%d/%Y")
> 
> But what I want to do is extract the year and month so I can construct
> an element in a ts object. Ideally I would like to see d$year but that
> doesn't seem to be available. Once I have a Date object how can I get
> an integer year?

as.numeric(format(d, format = "%Y"))
as.numeric(format(d, format = "%m"))

and so on.

Look at the formats in ?strftime for further details.

HTH

G

> 
> Thank you.
> 
> Kevin
> 
> ______________________________________________
> 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.
-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
 ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
 Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
 Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
 UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%




More information about the R-help mailing list