[R] Fetching datapoints from a Time Series

Achim Zeileis Achim.Zeileis at wu-wien.ac.at
Wed Oct 24 17:41:05 CEST 2007


One comment first: Sending a request to the list *once* is sufficient!

As for the question: A slight modification of Jim's solution...

> > x <- read.table(tc <- textConnection(" Date                        Value
> +  01/03/05 -0.008471364
> + 01/04/05 -0.008153802
> + 01/05/05 -0.000780031
> + 01/06/05 -0.000130064
> + 01/07/05 -0.000650576
> + 01/08/05 -0.000130166
> + 01/10/05 -0.004174282
> + 01/11/05 0.01027384
> + 01/12/05 -0.006099558
> + 01/13/05 -0.009811054
> + 01/14/05 0.000657073"), header=TRUE)
> > close(tc)
> > y <- zoo(x[,2], as.Date(as.character(x[,1]), '%m/%d/%y'))
> > y[index(y) >= as.Date('2005-01-02') & index(y) <= as.Date('2005-01-09')]
>   2005-01-03   2005-01-04   2005-01-05   2005-01-06   2005-01-07   2005-01-08
> -0.008471364 -0.008153802 -0.000780031 -0.000130064 -0.000650576 -0.000130166

A slightly shorter and maybe a bit more evocative solution (based on the
same text file/connection) is

  y <- read.zoo(tc, header = TRUE, format = "%m/%d/%y")
  window(y, start = as.Date("2005-01-02"), end = as.Date("2005-01-09"))

See also
  vignette("zoo", package = "zoo")
for more details.

hth,
Z



More information about the R-help mailing list