My zoo object I Would like to create a subset of values (these are discharge values) containing only December flow values.
Thank you!
My zoo object I Would like to create a subset of values (these are discharge values) containing only December flow values.
Thank you!
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")
library(zoo)
z1 <- zoo(1:100, order.by = seq(as.Date('1938-01-01'),
length.out=100, by = '1 week'))