[R] Working with temporal data [Solved]

Marc Schwartz marc_schwartz at comcast.net
Fri Feb 16 18:00:54 CET 2007


On Fri, 2007-02-16 at 16:47 +0000, Prof Brian Ripley wrote:
> 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
> ...

Yes, indeed....and just as I was thinking that I had consumed a
sufficient volume of coffee this morning...

Thanks!

Looking at the code for months() and weekdays(), for example:

> months.Date
function (x, abbreviate = FALSE) 
format(x, ifelse(abbreviate, "%b", "%B"))
<environment: namespace:base>

from a 'consistency' perspective, would it not make sense to have a
years.Date() function along the same lines as what I had proposed?

quarters() and julian(), of course, are appropriately different.

Regards,

Marc



More information about the R-help mailing list