[R] Working with temporal data [Solved]

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Feb 16 17:47:55 CET 2007


On Fri, 16 Feb 2007, Marc Schwartz wrote:

> On Fri, 2007-02-16 at 15:39 +0000, Sérgio Nunes wrote:
>> Just for the record, here are my steps for producing a date based histogram.
>> Data is stored in a file where each line only has a date - 2007/02/16
>>
>>> d<-readLines("filename.dat")
>>> d<-as.Date(d, format="%Y/%m/%d")
>>> pdf(yearly.pdf)
>>> hist(d, "years")
>>> dev.off()
>>
>> Instead of "years" you can also use "days", "weeks", "months", "secs",
>> "mins", "hours".
>>
>> One final question, how could I easily filter my dataset if, for
>> instance, I only wanted to see results from 2006 ?
>>
>> Thanks to all who helped,
>> Sérgio Nunes
>
> The easiest way may be to create your own "Year" extractor function,
> since there does not appear to be one by default, unless I missed it
> someplace (it is not listed in ?weekdays).

>From that help page

Note:

      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
      component.

> 1900+as.POSIXlt(d2)$year
[1] 2006 2004 2004
...

-- 
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 mailing list