Dear list, I'm afraid this is a mundane question. Here's the background: I've produced a function which allows me to sequentially measure angles and distances from a specified reference point to a curve defined by empirical data points while (i.e. using a while loop) the angle being measured is within certain bounds. Because the curve is circular I need to pars the data into basically an "upper" curve and "lower" curve. I tried to do this with an if statement, specifically: ycrit<-subset(data,subset=data$x==min(data$x) y.max<-length*sin(angle)+y.ref #length, angle and y.ref are given if(y.max<ycrit){ ........ } Now the problem: The while loop works for 4 iterations, until I get the error message: "Error in if (y.max < ycrit) { : missing value where TRUE/FALSE needed" When I forced the function to print y.max and ycrit for each iteration of the while loop, it returns finite real numbers, including ycrit = 153.5 and y.max = 245.16 for the step which returns the error message. Any ideas about what's going on here--why does R "think" that 245.16<153.5 is "missing," or is anything other than TRUE/FALSE? Am I using "if" incorrectly? In which case would it be more appropriate to perhaps create subsets of the data points based on < or > ycrit? Thanks in advance for any guidance-- Andy -- Andrew J. Rominger Department of Biological Sciences Stanford University rominger at stanford.edu
Hello - Andrew Rominger wrote:> Dear list, > > I'm afraid this is a mundane question. Here's the background: I've > produced a function which allows me to sequentially measure angles and > distances from a specified reference point to a curve defined by > empirical data points while (i.e. using a while loop) the angle being > measured is within certain bounds. Because the curve is circular I need > to pars the data into basically an "upper" curve and "lower" curve. I > tried to do this with an if statement, specifically: > > ycrit<-subset(data,subset=data$x==min(data$x)You might consider not using 'data' as a variable name (it is the name of a function in the utils package) , and clearer code might be ycrit <- subset(data, x == min(x)) But what does this return? It can definitely return a data.frame as in the following example: t1 <- data.frame(a = rep(2,2)) t1 class(subset(t1, a == min(a))) You probably do not want to them subsequently use this returned subset in an 'if' clause. Also, consider cases where there are NAs in the data: t2 <- data.frame(a = c(NA,rep(2,2))) t2 subset(t2, a == min(a)) This code returns a data.frame with 0 rows. I don't know what your specific problem is, but hopefully this might lead you in the right direction.> y.max<-length*sin(angle)+y.ref #length, angle and y.ref are given > if(y.max<ycrit){ > ........ > } > > Now the problem: The while loop works for 4 iterations, until I get the > error message: > > "Error in if (y.max < ycrit) { : missing value where TRUE/FALSE needed" > > When I forced the function to print y.max and ycrit for each iteration > of the while loop, it returns finite real numbers, including ycrit = > 153.5 and y.max = 245.16 for the step which returns the error message. > > Any ideas about what's going on here--why does R "think" that > 245.16<153.5 is "missing," or is anything other than TRUE/FALSE? Am I > using "if" incorrectly? In which case would it be more appropriate to > perhaps create subsets of the data points based on < or > ycrit? > > Thanks in advance for any guidance-- > Andy >
As the note at the bottom says provide commented, minimal, self-contained, reproducible code. It is difficult to comment on a loop when we have no code to read. --- On Fri, 7/11/08, Andrew Rominger <rominger at stanford.edu> wrote:> From: Andrew Rominger <rominger at stanford.edu> > Subject: [R] Help with error in "if then" statement > To: r-help at r-project.org > Received: Friday, July 11, 2008, 12:48 PM > Dear list, > > I'm afraid this is a mundane question. Here's the > background: I've > produced a function which allows me to sequentially measure > angles and > distances from a specified reference point to a curve > defined by > empirical data points while (i.e. using a while loop) the > angle being > measured is within certain bounds. Because the curve is > circular I > need to pars the data into basically an "upper" > curve and "lower" > curve. I tried to do this with an if statement, > specifically: > > ycrit<-subset(data,subset=data$x==min(data$x) > y.max<-length*sin(angle)+y.ref #length, angle and y.ref > are given > if(y.max<ycrit){ > ........ > } > > Now the problem: The while loop works for 4 iterations, > until I get > the error message: > > "Error in if (y.max < ycrit) { : missing value > where TRUE/FALSE needed" > > When I forced the function to print y.max and ycrit for > each iteration > of the while loop, it returns finite real numbers, > including ycrit = > 153.5 and y.max = 245.16 for the step which returns the > error message. > > Any ideas about what's going on here--why does R > "think" that > 245.16<153.5 is "missing," or is anything > other than TRUE/FALSE? Am I > using "if" incorrectly? In which case would it > be more appropriate to > perhaps create subsets of the data points based on < or > > ycrit? > > Thanks in advance for any guidance-- > Andy > > -- > Andrew J. Rominger > Department of Biological Sciences > Stanford University > rominger at stanford.edu > > ______________________________________________ > 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.__________________________________________________________________ [[elided Yahoo spam]]