How to subset xts object based upon [is not] condition

佐手、 提交于 2019-12-08 00:23:47

问题


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

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

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


回答1:


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

Regardless, one way to accomplish what you want is to use the which.i argument to [.xts to find the integer indices. Then you can remove those observations from your xts object by using a 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],]


来源:https://stackoverflow.com/questions/32029478/how-to-subset-xts-object-based-upon-is-not-condition

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!