[R] Using diff, ifelse on zoo object
charliegenge
charlie.genge at sc.com
Mon Oct 12 11:30:40 CEST 2009
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.
More information about the R-help
mailing list