-2

My zoo object I Would like to create a subset of values (these are discharge values) containing only December flow values.

Thank you!

Jaap
  • 81,064
  • 34
  • 182
  • 193
Fashraf
  • 21
  • 3
  • Do not post your data as an image, please learn how to give a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610) – Jaap Jan 31 '16 at 17:32

1 Answers1

2

We can extract the 'months' from the index with format, get a logical index by comparing with 'Dec', and use that to subset the zoo object.

z1[format(index(z1), '%b')=='Dec']
#1938-12-03 1938-12-10 1938-12-17 1938-12-24 1938-12-31 
#    49         50         51         52         53 

If we convert to xts object, .indexmon from the xts package can be also used. The .indexmon starts from 0, so December is 11.

library(xts)
z1[.indexmon(as.xts(z1))==11]

Other options from the comments are using grep on the index to get the numeric index and subset (from @Pierre Lafortune)

 z1[grep("-12-",index(z1))]

Or with subset/month option (from @ G. Grothendieck)

 subset(z1, months(time(z1)) == "December") 

data

library(zoo)
z1 <- zoo(1:100, order.by = seq(as.Date('1938-01-01'), 
              length.out=100, by = '1 week'))
akrun
  • 874,273
  • 37
  • 540
  • 662