[R] difference between <- and =

Richard A. O'Keefe ok at cs.otago.ac.nz
Tue Sep 2 01:18:43 CEST 2003

Spencer Graves <spencer.graves at PDF.COM> wrote:
	A common, punishing error for me, with DF being a data frame, is the 
		  if(DF$a = 1) ...
	 where I intended to write "if(DF$a == 1)...".

This is a syntax error in R.

    > d <- data.frame(a=2,b=3,c=4)
    > if (d$a=1) cat("foo!")
    Error: syntax error

If I've understood the R documentation correctly, "=" is only allowed
as a synonym for "<-" at "statement" level.

Of course, that's one more reason not to use "=" when you DO want
assignment; "<-" has no such restriction.

More information about the R-help mailing list