[R] Working with temporal data [Solved]

Marc Schwartz marc_schwartz at comcast.net
Fri Feb 16 17:08:51 CET 2007


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

For example, using your data from my prior reply:

> d2
[1] "2006-08-09" "2004-02-11" "2004-06-09"


Years <- function(x) format(x, "%Y")


> Years(d2)
[1] "2006" "2004" "2004"


> Years(d2) == 2006
[1]  TRUE FALSE FALSE


> d2[Years(d2) == 2006]
[1] "2006-08-09"


So, just use something like:

 hist(d2[Years(d2) == 2006], "years")

HTH,

Marc Schwartz



More information about the R-help mailing list