You can use 'rle' to find the 'runs' of decreasing values by looking at the
'diff'. You can change the values to get the different lengths of runs.

> # generate some test data
> set.seed(3)
> x <- runif(100)
> # use 'diff' to determine if decreasing
> x.dec <- diff(x) < 0
> # use rle to find 'runs' of 3 TRUEs
> x.rle <- rle(x.dec)
> sum(x.rle$length == 3 & x.rle$values) # how many occurances
[1] 6
> # determine the indices in the x.rle vector
> x.loc <- which(x.rle$lengths == 3 & x.rle$values)
> # print out the location of occurance
> cumsum(x.rle$lengths)[x.loc]
[1] 21 32 46 58 69 83
> x  # print out data for comparison
  [1] 0.168041526 0.807516399 0.384942351 0.327734317 0.602100675
0.604394054 0.124633444 0.294600924
  [9] 0.577609919 0.630979274 0.512015898 0.505023914 0.534035353
0.557249436 0.867919488 0.829708693
 [17] 0.111449153 0.703688359 0.897488264 0.279732554 0.228201881
0.015329893 0.128981559 0.093381929
 [25] 0.236885007 0.791147409 0.599731566 0.910147711 0.560424554
0.755704769 0.379171891 0.373280977
 [33] 0.170290643 0.453307324 0.258413960 0.336265952 0.889583035
0.201946299 0.579186043 0.207632030
 [41] 0.281468792 0.786281204 0.173019349 0.570747518 0.419282963
0.267622165 0.047809442 0.103493054
 [49] 0.314031456 0.800641062 0.229324695 0.212998441 0.877100906
0.993221963 0.844247020 0.910436549
 [57] 0.471269732 0.224418408 0.127814657 0.279683512 0.816106060
0.057612993 0.802829250 0.104387835
 [65] 0.766606019 0.304810566 0.769287421 0.540655726 0.362370532
0.092556916 0.759755255 0.760819947
 [73] 0.903260845 0.966282786 0.515256623 0.549480674 0.163719897
0.164596954 0.786332777 0.751113420
 [81] 0.784214704 0.654354915 0.378104397 0.008566967 0.955329554
0.838616148 0.213424938 0.494713556
 [89] 0.636244256 0.921091393 0.011744041 0.267402917 0.435572102
0.829467095 0.870944038 0.251068959
 [97] 0.324358248 0.306237812 0.184282035 0.679977400
>



On 2/21/07, Alfonso Sammassimo <cincinattikid@bigpond.com> wrote:
>
> 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.
>
> ______________________________________________
> R-help@stat.math.ethz.ch 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?

	[[alternative HTML version deleted]]

