Re: [R] while loop syntax help

From: jim holtman <jholtman_at_gmail.com>
Date: Fri, 29 Feb 2008 16:15:49 -0500

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.

______________________________________________
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.
Received on Fri 29 Feb 2008 - 21:21:32 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 29 Feb 2008 - 21:30:17 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive