0

For example, I have two ZOO objects:

x <- zoo(matrix(1:10, 10, 2), 1:10)
y <- zoo(matrix(11:20, 10, 2), 5:15)

I want to get first index value, which have both ZOO objects. In this example it must be index value of 5. Because index of 5 have both objects and this is most recent index.

I can loop index(x) vector and with inner loop compare every index(y) element with index(x) elements, but this looks ugly. Can i do that without looping? Thanks.

UPDATE: I found that I can do following:

idx_val <- head(intersect(index(x), index(y)), 1)

This is clear and fast solution? If yes, then question is closed.

Eldar Agalarov
  • 4,849
  • 4
  • 30
  • 38
  • 1
    That seems like a good solution. Here is another one `index(na.omit(merge(x, y)))[1]` . Note that it differs in the case that there are leading NAs in `x` and `y` so which one to use would depend on what you want. – G. Grothendieck Apr 17 '14 at 11:14
  • Yes, I need to remove leading NAs, so your variant is better. Thanks! – Eldar Agalarov Apr 18 '14 at 05:44

1 Answers1

1

Have transferred this from comment to here and improved slightly. This approach differs from the one posted in the question in the case that there are leading NA values in x or y :

start(na.omit(merge(x, y)))
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341