[R-SIG-Finance] Omiting some observation of a zoo object
Gabor Grothendieck
ggrothendieck at gmail.com
Wed Feb 16 15:39:10 CET 2011
On Wed, Feb 16, 2011 at 9:48 AM, Bogaso Christofer
<bogaso.christofer at gmail.com> wrote:
> Hi there, I would like to omit some observations of a zoo vector
> corresponding with some defined dates. So I wrote following codes:
>
>
>
>> set.seed(1)
>
>> dates <- seq(as.Date("2011-01-01"), as.Date("2011-01-31"), by="1
> day")[-c(7,8,9,20,21,22)]
>
>> mySeries <- zoo(rnorm(25), dates)
>
>> mySeries
>
> 2011-01-01 2011-01-02 2011-01-03 2011-01-04 2011-01-05 2011-01-06
> 2011-01-10 2011-01-11 2011-01-12 2011-01-13 2011-01-14 2011-01-15
>
> -0.62645381 0.18364332 -0.83562861 1.59528080 0.32950777 -0.82046838
> 0.48742905 0.73832471 0.57578135 -0.30538839 1.51178117 0.38984324
>
> 2011-01-16 2011-01-17 2011-01-18 2011-01-19 2011-01-23 2011-01-24
> 2011-01-25 2011-01-26 2011-01-27 2011-01-28 2011-01-29 2011-01-30
>
> -0.62124058 -2.21469989 1.12493092 -0.04493361 -0.01619026 0.94383621
> 0.82122120 0.59390132 0.91897737 0.78213630 0.07456498 -1.98935170
>
> 2011-01-31
>
> 0.61982575
>
>> toOmit <- as.Date(c("2011-01-07", "2011-01-08", "2011-01-09"))
>
>> mySeries[-toOmit]
>
> Error in `-.Date`(toOmit) : unary - is not defined for Date objects
>
Here are three ways:
library(zoo)
## set up input data
set.seed(1)
mySeries <- zooreg(rnorm(25), as.Date("2011-01-01"))
toOmit <- as.Date(c("2011-01-07", "2011-01-08", "2011-01-09"))
## method 1. window.zoo
window(mySeries, setdiff(time(mySeries), toOmit))
## method 2. %in%
mySeries[! time(mySeries) %in% toOmit ]
## method 3. na.omit - if mySeries has no NAs
na.omit(replace(mySeries, toOmit, NA))
--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com
More information about the R-SIG-Finance
mailing list