[R] Is there better alternative to this loop?

Alfonso Sammassimo cincinattikid at bigpond.com
Thu Feb 22 02:33:08 CET 2007


Dear List,

Thanks to those who helped with my enquiry a few days ago.

I have a another question on loops, in this case I am trying to print out 
the row of a data frame if the previous 3 values (daily values) in col5 are 
in descending order. I have this loop which works, but ask whether this can 
be done differently (without conventional loop) in R:

flag="T"
d= 3 # d represents previous down days
for(i in (d+1): 100)
{
for( j in (i-d):(i-1))
{
if(x[j,5]<x[i,5]){flag="F"}
}
if( flag == "T"){ print(x[i,1])}
flag="T";
}

Any help appreciated,

Regards,
Alf Sammassimo
Melbourne, Australia.



More information about the R-help mailing list