How to multiply an xts object based on the condition [is not]

I have an xts object, now I would like to select all the rows of the index except for a certain period.

I understand that my.object["2015/2015-03-01"] will select index rows from 2015 to March 2015. But how can I do an operation not based on the same xts syntax?

I tried my.object[!"2015/2015-03-01"] , but it does not work.

+2
r xts
Aug 15 '15 at 21:21
source share
1 answer

I'm not sure why you expect my.object[!"2015/2015-03-01"] to work. Applying a logical function to a character string does not make sense.

Regardless, one way to achieve what you want is to use the which.i argument to [.xts to find integer indices. You can then remove these observations from your xts object using negative i in another call to [.xts .

 R> require(xts) R> data(sample_matrix) R> x <- as.xts(sample_matrix) R> unwantedObs <- x["2007-01-04/2007-06-28", which.i=TRUE] R> x[-unwantedObs,] Open High Low Close 2007-01-02 50.03978 50.11778 49.95041 50.11778 2007-01-03 50.23050 50.42188 50.23050 50.39767 2007-06-29 47.63629 47.77563 47.61733 47.66471 2007-06-30 47.67468 47.94127 47.67468 47.76719 R> # in one line: R> #x[-x["2007-01-04/2007-06-28", which.i=TRUE],] 
+4
Aug 15 '15 at 21:40
source share



All Articles