[R] Problem with parser and if/else
Thomas Lumley
tlumley at u.washington.edu
Fri Nov 14 16:24:28 CET 2003
On Fri, 14 Nov 2003, James MacDonald wrote:
>
> There are no violations to that rule in either lm or aov. It simply
> looks that way if you type lm or aov from within R. If you looked at the
> functions themselves using an editor, you would see that they all
> conform to
>
> if{
> ...
> }else{
> ...
> }
>
This turns out not to be the case. Looking for example at
lm() in src/library/base/R/lm.R
if (is.empty.model(mt)) {
x <- NULL
z <- list(coefficients = numeric(0), residuals = y,
fitted.values = 0 * y, weights = w, rank = 0,
df.residual = length(y))
if(!is.null(offset)) z$fitted.values <- offset
}
else {
x <- model.matrix(mt, mf, contrasts)
z <- if(is.null(w)) lm.fit(x, y, offset = offset,
singular.ok=singular.ok, ...)
else lm.wfit(x, y, w, offset = offset, singular.ok=singular.ok,
...)
}
The *real* rule is that the } of the if() must not produce a syntactically
complete statement, so else on a separate line is ok inside functions.
I still tend to write } else {, but I'm sure it annoys some people.
-thomas
More information about the R-help
mailing list