if I have a variable of type "POSIXt" "POSIXct" like "1969-12-31 19:00:01 EST" how can I dynamicly get the year (or years if vector) and month?
On Thu, 9 Dec 2004, Omar Lakkis wrote:> if I have a variable of type "POSIXt" "POSIXct" like "1969-12-31 > 19:00:01 EST" how can I dynamicly get the year (or years if vector) > and month?See ?months, or any other official documentation on dates in R. -- 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
Omar Lakkis <abu3ammar <at> gmail.com> writes: : : if I have a variable of type "POSIXt" "POSIXct" like "1969-12-31 : 19:00:01 EST" how can I dynamicly get the year (or years if vector) : and month? Enter unclass(as.POSIXlt(x)) to see the components available to you when using POSIXlt. These include the year component which is the year minus 1900 and the mon component which is the month number noting that January is 0, February is 1, etc. Also note that the year and month can differ in different time zones so you may need tz = "GMT" if you need it with respect to GMT time, say. You can also use something like format(x, "%y-%m"). tz= comment applies here too. ?strptime lists the various % codes. You may be better off using Date class if your granularity is year and month or if you have regularly spaced monthly data you can use a ts series with frequency of 12. The article in RNews 4/1 gives many more examples and pointers to additional information.