[R] Another R syntax question

Thomas Lumley tlumley at u.washington.edu
Wed Sep 3 16:10:44 CEST 2003

On Wed, 3 Sep 2003, Richard A. O'Keefe wrote:

> The file src/library/base/R/print.R
> contains this line:
>     x0 <- xm[okP]==0 != (as.numeric(Cf[okP])==0)
> I didn't know R allowed that, and I wonder if it is deliberate?

Well, I'm not surprised that it's syntactically valid, but I wouldn't know
off-hand which way it evaluated.

> In mathematics, you would expect x = y not= z to mean
> (x = y) and (y not= z).;
> In R, it is parsed as (x == y) != z.

I think it's clear that it will parse as either
(x==y) != z
x == (y!=z)
but not which.

> While I have to admit that the spacing around == and != in print.R's
> example does actually convey the actual association, in a vaguely
> Principia Mathematica-ish way, I have to wonder:
> Is R really seriously intended to swallow x == y != z without complaint?
> If the line
> %left       GT GE LT LE EQ NE
> in src/main/gram.y were changed to
> %nonassoc   GT GE LT LE EQ NE
> then the line
>     x0 <- xm[okP]==0 != (as.numeric(Cf[okP])==0)
> in src/library/base/R/print.R would have to be changed to
>     x0 <- (xm[okP]==0) != (as.numeric(Cf[okP])==0)
> and all things considered, surely that would be an improvement?

This is presumably S compatible (<checks: yes it is>) and so while
changing print.R would be an improvement, changing the grammar probably


More information about the R-help mailing list