[Rd] Re: [R-sig-finance] syntax for a loop

Patrick Burns patrick at burns-stat.com
Sun Nov 28 13:10:44 CET 2004


I don't think I understand you entirely, but here are a couple
things.

You need to shorten the loop so you don't run off the end.

You basically just write the code as you've stated it except that
"and" is && for single values and & for vectors

You don't say what should happen if the test is not true.

You can probably use "ifelse" instead of  a loop.

iseq <- 2:(length(x.dif) - 2)
test <- x.dif[iseq-1] <= 0 & x.dif[iseq] > 0 & x.dif[iseq + 2] > 0
ifelse(test, iseq + 2, NA)

I suspect that the last term in test should be i+1 and not i+2 ?

You can see S Poetry for details.

Patrick Burns

Burns Statistics
patrick at burns-stat.com
+44 (0)20 8525 0696
http://www.burns-stat.com
(home of S Poetry and "A Guide for the Unwilling S User")

ebashi wrote:

>I'll appreciate if some one can help me with the
>following loop. This is the logic of the loop,
>if we have the following data;
>  
>
>>x.df
>>    
>>
>    x.dif
> .    .
> .    .
>102  0.00
>103  0.42
>104  0.08
>105  0.00
>106  0.00
>107  0.00
>108 -0.16
>109 -0.34
>110  0.00
>111 -0.17
>112 -0.33
>113  0.00
>114  0.00
>115  0.00
>116  0.33
>117  0.17
>118  0.00 
> .    .
> .    .
>I'm trying to find i's where 
>  for (i in 2:length(x.dif))
>  if (x.dif[i-1]<=0 and x.dif[i]>0 and x.dif[i+2]>0)
>  it would return i+2 to me,
>How can I turn this to a right format as a loop.(I
>can't figure out the syntax)
>
>Cheers,
>
>Sean
>
>_______________________________________________
>R-sig-finance at stat.math.ethz.ch mailing list
>https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>
>
>  
>



More information about the R-devel mailing list