How do I get all the dates with a particular time from a zoo object?

Related searches

Suppose I have a zoo object of date/times (POSIXct) as my index and values:

...                     val
2006-08-01 10:00:00      1
2006-08-01 10:10:00      2
2006-08-01 10:20:00      3
2006-08-01 10:30:00      4
2006-08-02 10:00:00      5
2006-08-02 10:10:00      6
2006-08-02 10:20:00      7
2006-08-02 10:30:00      8

This should be simple but I can't think of how to get a list of all dates/times where let's say the time is 10:10:00am ...

Sample zoo data object:

Lines <- "datetime val
2006-08-01T10:00    1
2006-08-01T10:10    2
2006-08-01T10:20    3
2006-08-01T10:30    4
2006-08-01T10:40    10
2006-08-02T10:10    5
2006-08-02T10:20    6
2006-08-02T10:30    7
2006-08-02T10:40    8"

z <- read.zoo(text = Lines, tz = "", header = TRUE, format = "%Y-%m-%dT%H:%M")

NOTE: I would prefer a base-R/zoo implementation but other packages would be interesting to see

A way is to use lubridate to extract time components. This assumes second() is equal to 0.


z[hour(index(z)) == 10 & minute(index(z)) == 10]

You can also coerce to character and then search for the specific time.

z[grep("10:10:00", index(z))]

Out of curiosity, I ran a benchmark. The results are presumably due to the type conversion and are obviously insignificant on a small data set.

It slows down as you add more criteria though. If you add in seconds(index(z)) == 0, then the lubridate method is basically the same. The grep solutions are probably the way to go.


benchmark <- microbenchmark(
  lubridate = z[hour(index(z)) == 10 & minute(index(z)) == 10],
  grep = z[grep("10:10:00", index(z))]

Manipulating Time Series Data in R with xts & zoo, Given that ex_matrix is a matrix, extract the 3rd row and 2nd column. Use the xts constructor to create an object called smith using data and dates as the The index holds all the information we need for xts to treat our data as a time series. How to get the date of specific value within a zoo object?. Hi all, I have a zoo time series object, see below R › R help.

We can use base::strftime:

z[strftime(z, format="%H:%M:%S") == strftime("1000-01-01 10:10:00", format="%H:%M:%S")]

 #> 2006-08-01 10:10:00 2006-08-02 10:10:00 
 #>                   2                   5 

zoo function, If this is not the case for a particular index/date/time class, then methods for these objects can be extracted by coredata (strips off all "zoo" -specific attributes)� In part two of this exercise, you will read the same data into a zoo object using read.zoo and then convert the zoo object into an xts object. The data in this exercise are quite simple, but will require some effort to properly import and clean. The full name of the file you will be working with has been saved as the value of tmp_file.

Use grep or grepl like this. No additional packages are used.

z[grepl("10:10:00", time(z))]
## 2006-08-01 10:10:00 2006-08-02 10:10:00 
##                   2                   6

Another approach is to use substr or substring:

z[substring(time(z), 12, 20) == "10:10:00"])
## 2006-08-01 10:10:00 2006-08-02 10:10:00 
##                   2                   6

If we knew the positions then another possibility is to use them:

z[seq(2, length(z), 4)]
## 2006-08-01 10:10:00 2006-08-02 10:10:00 
##                   2                   6

The input in reproducible form was assumed to be the following. (It seems that the code below was copied to the question.)

Lines <- "datetime        val
2006-08-01T10:00:00      1
2006-08-01T10:10:00      2
2006-08-01T10:20:00      3
2006-08-01T10:30:00      4
2006-08-02T10:00:00      5
2006-08-02T10:10:00      6
2006-08-02T10:20:00      7
2006-08-02T10:30:00      8"

z <- read.zoo(text = Lines, header = TRUE, tz = "", format = "%Y-%m-%dT%T")

[PDF] zoo Quick Reference, To return all dates corresponding to a series index(z) or equivalently R> plot(m[ , 2:3], plot.type = "single", col = c("red", "blue"), lwd = 2) Here is another example using the same zsec zoo object but this time rather than aggregating. index is a generic function for extracting the index of objects, currently it has a default method and a method for zoo objects which is the same as the time method for zoo objects. Another pair of generic functions provides replacing the index or time attribute. Methods are available for "zoo" objects only, see examples below.

xts Cheat Sheet: Time Series in R, Get started on time series in R with this xts cheat sheet, with code examples. handling of many R time series classes by extending zoo , which is the package coredata: always a matrix for xts objects; index: vector of any Date Replace the value at 1 specific index with NA Get all data until March '54 To do this, they first have to determine the month, by extracting the months from the datetime object. An easy way to achieve this is to work with dates in the POSIXlt class, because this type of data is stored internally as a named list, which enables you to extract elements by name.

Time Series in R, The Power of xts and zoo, 'Date'. Time-based indices. xts objects get their power from the index attribute xts provides methods to convert all of the major objects you are likely to come to a malfunctioning sensor on a particular day or a set of outliers given a holiday. xts or the Extensible Time Series is one of such packages that offers such a time series object. It's a powerful R package that provides an extensible time series class, enabling uniform handling of many R time series classes by extending zoo , which is the package that is the creator for an S3 class of indexed totally ordered observations

as.Date will accept numeric data (the number of days since an epoch), but only if origin is supplied. The format and as.character methods ignore any fractional part of the date. Value. The format and as.character methods return a character vector representing the date. NA dates are returned as NA_character_. The as.Date methods return an object

  • I would prefer not to parse out the time string but say "give me all rows where the time is 10:10:00"
  • Wow, that's nice!
  • The lubridate code assumes the seconds in the data is always 0 whereas the grep code does not.