Skip to content
Prev 247703 / 398503 Next

Time and xts

Nicos, 

there are certainly better / faster methods to compare a time range which I 
simply don't know of, but the condensed code below should do it. Your main 
problem was that you can't compare time(x) to 7:55, since the 
latter expression denotes a sequence from 7 to 55 in R. Consequently, such
an expression compares time(x) with all integer numbers between 7 and 55:
[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[25] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[37] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[49] FALSE
Your while loop was therefore equivalent to
Best 

Hugo

-- snip --
 library(zoo)
library(chron)

Lines<- "Date    Time    Open    High    Low     Close
1/2/2005        17:05   1.3546  1.3553  1.3546  1.35495
1/2/2005        17:10   1.3553  1.3556  1.3549  1.35525
1/2/2005        17:15   1.3556  1.35565 1.35515 1.3553
1/2/2005        17:25   1.355   1.3556  1.355   1.3555
3/14/2006       5:21    1.18895 2.18925 1.18835 1.1885
3/14/2006       7:56    2.18895 3.18925 1.18835 1.1885
2/13/2006       5:20    1.18895 1.18925 1.18835 1.1885"

z<- read.zoo(con<-textConnection(Lines), header = TRUE, index = list(1, 2), FUN = function(d,t) as.chron(paste(d, t), format = "%m/%d/%Y %H:%M"))
close(con);

entrytrade <- function(k) { k$Open+k$High }

#Note: A binary and operator (&) instead a logical (&&) is needed here
new <- entrytrade( z[hours(time(z)) >= 2 & ( hours(time(z)) + minutes(time(z))/100 <= 7.55 ) ] );

print(new);

--- snip ---
On Tuesday 18 January 2011 22:36:12 rnick wrote: