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

假如想象 提交于 2019-12-06 12:02:17

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