0

I am trying to plot a chart of some data I have loaded into R. But I only want a chart from one specific time period to another. How does one restrict on the sample of the data for certain time periods, say from 09/20/2010 to 09/23/2010.

If I used the "xts" package in R, I think one can simple state:

plot(dat$weight['2010-09-20/2010-09-23'])

But this format isn't working on the zoo package. Can anyone help?

library(zoo)    
dat=read.zoo("filelocation",header=T,colClasses=c("Date","numeric"))

  date   weight
2010-10-04 52495    
2010-10-01 53000    
2010-09-30 52916    
2010-09-29 52785    
2010-09-28 53348    
2010-09-27 52885    
2010-09-24 52174    
2010-09-23 51461    
2010-09-22 51286    
2010-09-21 50968    
2010-09-20 49250
gabriel
  • 399
  • 2
  • 7
  • 17
  • 1
    If it were an xts, the xts subsetting string would be "2010-09-20/2010-09-23", not "09-20-2010/09/23/2010". – GSee Dec 29 '12 at 22:26
  • Thanks GSee, I just looked at the ?xts documentation. It reads: The time must be ‘left-filled’, that is to specify a full year one needs only to provide the year, a month would require the full year and the integer of the month requested - e.g. '1999-01'. This format would extend all the way down to seconds - e.g. '1999-01-01 08:35:23'. Leading zeros are not necessary. – gabriel Dec 29 '12 at 22:37
  • 1
    That's not what @GSee is referring to. His point is that the datetime string must be ISO-8601 compliant, which yours is not. – Joshua Ulrich Dec 29 '12 at 23:03

1 Answers1

2
dat[index(dat) >= "2010-09-20" & index(dat) <= "2010-09-23"]

2010-09-20 2010-09-21 2010-09-22 2010-09-23 
     49250      50968      51286      51461 
Matthew Lundberg
  • 42,009
  • 6
  • 90
  • 112
  • Thanks Matt. so the as.Date() function isn't necessary because I recognized the data at timeseries via the colClasses option in the read command? – gabriel Dec 29 '12 at 22:39
  • That's correct. The `character` values will be coerced to `Date` to do the comparison. See `?Ops.Date`. – Matthew Lundberg Dec 29 '12 at 22:52
  • Thank you for the help Matt. R can be a struggle. I don't understand why different packages do the same operations with different functions and formats. One would think the format to specify a subset in xts would extend to the zoo package but that's not the case – gabriel Dec 29 '12 at 23:02
  • 1
    @gabriel, if you read the [`xts` vignette](http://cran.r-project.org/web/packages/xts/vignettes/xts.pdf), you'll see that xts extends zoo, not the other way around. (see section 2.1) – GSee Dec 29 '12 at 23:14