[R] while loop syntax help

jim holtman jholtman at gmail.com
Fri Feb 29 22:15:49 CET 2008


Does this give the answer that you want?

> x <- c(5,5,7,6,5,4,3)
> result <- NULL
> for (i in 1:(length(x) - 2)){
+     if ((x[i + 1] < x[i]) && (x[i + 2] < x[i])) result <- c(result, i)
+ }
> result
[1] 3 4 5
>



On 2/29/08, zack holden <zack_holden at hotmail.com> wrote:
>
> Dear list,
> I'm trying to write my first looping function in R. After many hours of searching help files and previous posts, I'm at wits end. Please forgive my programming ignorance...any help is greatly appreciated.
>
> I need to sort through a vector (x) and identify the point at which 2 successive values become smaller than the previous value.
>
> I've written a "while" statement that I think should work. It's should basically say: If value 1 > value 2 and also > value3, then == row(Value 1). Else, go to the next Value. However, output returns "NULL", no matter how I've modified the syntax.
>
> Thanks in advance for any help.
>
> Zack
> #############################
> x <- c(5,5,7,6,5,4,3) x <- data.frame(x)
> y <-length(x)-2counter <- 1
> output = c()
> while(counter <= y) {
>
> counter1 <- counter+1counter2 <- counter+2
> if(x[counter,1] > x[counter1,1]|| x[counter1,1] > x[counter2,1]){output = x[counter, ]
> } else {
> counter = counter+1
> }
> counter = y}########################################
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org 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?  Tell me what you want to
do, not how you want to do it.



More information about the R-help mailing list