[R] Using diff, ifelse on zoo object

charliegenge charlie.genge at sc.com
Tue Oct 13 11:56:57 CEST 2009


Thanks very much Gabor! Worked like a treat. Good to know that the na's
really mess up the ifelse function.

Thanks again.




Gabor Grothendieck wrote:
> 
> The problem is that ifelse does not work the way you might think (see
> value section of ?ifelse) and basically should not be used with three
> zoo objects unless the three arguments to ifelse have the same time
> index.
> 
> We can get that effect by using na.pad = TRUE in your diff call:
> 
>    TradedRate <- with(x, ifelse(abs(diff(x.POS, na.pad = TRUE)) > 0,
> ifelse(x.POS != 1, -x, x), NA))
> 
> or alternately we can merge them together first:
> 
>    xm <- merge(x, dif = abs(diff(x$x.POS)))
>    TradedRate <- with(xm, ifelse(dif > 0, ifelse(x.POS != 1, -x, x), NA))
> 
> By the way, to ensure that your output is reproducible be sure to use
> set.seed(...) before any call to a random number generator when
> posting.
> 
> 
> On Mon, Oct 12, 2009 at 5:30 AM, charliegenge <charlie.genge at sc.com>
> wrote:
>>
>> Hi,
>>
>> I'm having an issue when using diff and ifelse on a zoo object.....
>>
>> x.Date <- as.Date("2003-02-01") + c(1, 3, 7, 9, 14) - 1
>> x <- zoo(rnorm(5), x.Date)
>> x.POS <- c(0,0,0,1,1)
>> x<- merge(x,x.POS)
>> x
>>                        x         x.POS
>> 2003-02-01   -0.1858136     0
>> 2003-02-03   -1.3188533     0
>> 2003-02-07    0.2709794      0
>> 2003-02-09   -1.4915262     1
>> 2003-02-14    0.5014170      1
>>
>> When I create this new zoo object using the previous one (x) I don't get
>> exactly what I need......the traded rate is based on the lagged values,
>> rather than the present ones...
>>
>> TradedRate <- ifelse(abs(diff(x[,"x.POS"],lag= 1))>0,ifelse(x[,"x.POS"]
>> !=1,-x[,"x"],x[,"x"]),NA)
>> x <- merge(x, TradedRate, all=TRUE)
>> x
>>                    x x.POS TradedRate
>> 2003-02-01 -0.1858136     0         NA
>> 2003-02-03 -1.3188533     0         NA
>> 2003-02-07  0.2709794     0         NA
>> 2003-02-09 -1.4915262     1 -0.2709794
>> 2003-02-14  0.5014170     1         NA
>>
>> The value for TradedRate on the 9th Feb should be -1.4945262 instead of
>> -0.2709794........what am I doing wrong?
>>
>> Thanks very much..
>> --
>> View this message in context:
>> http://www.nabble.com/Using-diff%2C-ifelse-on-zoo-object-tp25852822p25852822.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 
> 

-- 
View this message in context: http://www.nabble.com/Using-diff%2C-ifelse-on-zoo-object-tp25852822p25870058.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list