Search code examples
rxts

Comparing xts index elements


I have two xts objects, R (containing monthly returns) and W (containing a monthly binary indicator showing if a stock is part of an index).

I have created two subsequent variables:

x <- index(W)
y <- index(R) 

How do I find the position of element x[1] is in y? Assuming x is a subset of y.

The example below shows the format of x and y.

> head(x)
[1] "2002-11-29 UTC" "2002-12-31 UTC" "2003-01-31 UTC" "2003-02-28 UTC"
[5] "2003-03-31 UTC" "2003-04-30 UTC"
> head(y)
[1] "2000-02-29 UTC" "2000-03-31 UTC" "2000-04-28 UTC" "2000-05-31 UTC" "2000-06-30 UTC"
[6] "2000-07-31 UTC"

I would like to find the position of i where x[1] == y[i]

> x[1]
[1] "2002-11-29 UTC"

Solution

  • You can use function which. Here is example with simulated data:

        x<- c(1,2,3,4)
        y<-c(1,2,3,4,5,6,7,8)
    
        which(y==x[1])