R: A subset of the zoo facility?

Given the date, I can access the corresponding element in the zoo vector. For instance:

z[as.POSIXct(1213708500, origin="1970-01-01")] 

it returns

 2008-06-17 14:15:00 -8.28123 

I would like to get a vector of 30 consecutive elements (ending with the element above).

How can I do this (efficiently) without knowing the timestamp of the start element?

I know that I can do this using the window function, but this requires a start time and an end time.

+4
source share
1 answer

Use something like

 ind <- which(index(z)==as.POSIXct(1213708500, origin="1970-01-01")) + seq(-29,0) 

followed by

 z[ind] 

where which() gives you a matching index from which you can select thirty consecutive elements by normal indexing.

+4
source

Source: https://habr.com/ru/post/1394805/


All Articles