[R] Year and Month extraction from Date object.
Prof Brian Ripley
ripley at stats.ox.ac.uk
Tue Oct 27 22:56:16 CET 2009
Other components such as the day of the month or the year are very
easy to compute: just use ‘as.POSIXlt’ and extract the relevant
(d <- Sys.Date())
1900 + as.POSIXlt(d)$year
1 + as.POSIXlt(d)$mon
should get you started.
On Tue, 27 Oct 2009, rkevinburton at charter.net wrote:
> 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?
> Thank you.
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-help