On Tue, 8 Feb 2005, Christian Lederer wrote: > can anybody explain the reason, why the first piece of code > below gives a parsing error, while the other two variations > work? > > # Gives a parsing error > x <- 1 > if (x < 0) > { > y <- 1 > } > else # Error occurs at this line > { > y <- -1 > } > > > # This works > x <- 1 > { > if (x < 0) > { > y <- 1 > } > else > { > y <- -1 > } > } > > # This works too > x <- 1 > if (x < 0) > { > y <- 1 > } else > { > y <- -1 > } >
help("if") In the first case, the if expression is complete prior to the else, hence the error. The second case completes since the outer enclosing brace has not been processed. The third case completes since the else is on the same line as brace closing if expression and else expression not yet processed. There may also be issues whether you're running interactively or not. Recommend not using Whitesmith style brace formatting with R or S-plus. IMO, the following formatting style works best: sety <- function(x) { if (x < 0) { y <- 1 } else { y <- -1 } } ---------------------------------------------------------- SIGSIG -- signature too long (core dumped) ______________________________________________ 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