[R] advice/opinion on "<-" vs "=" in teaching R
Berwin A Turlach
berwin at maths.uwa.edu.au
Fri Jan 15 11:22:34 CET 2010
G'day all,
On Fri, 15 Jan 2010 09:08:44 -0000 (GMT)
(Ted Harding) <Ted.Harding at manchester.ac.uk> wrote:
> On 15-Jan-10 08:14:04, Barry Rowlingson wrote:
> >> [...]
> > I would regard modifying a variable within the parameters of a
> > function call as pretty tasteless. What does:
> >
> > foo(x<-2,x)
> > or
> > foo(x,x<-3)
> >
> > do that couldn't be done clearer with two lines of code?
Depending on the definition of foo() more than two lines might be
necessary to clarify what this code is actually supposed to do. :)
> > Remember: 'eschew obfuscation'.
Absolutely.
> Tasteless or not, the language allows it to be done; and therefore
> discussion of distinctions between ways of doing it is relevant to
> Erin's question!
And a full discussion of these examples would include to warn about the
side effects of lazy evaluation:
R> rm(list=ls(all=TRUE))
R> foo <- function(x, y){
+ if(x > 0 ) x else x + y}
R> foo(2, x <- 10)
[1] 2
R> x
Error: object 'x' not found
R> foo(-2, x <- 10)
[1] 8
R> x
[1] 10
Having said this, I often use "plot(fm <- lm(y ~ . , somedata))".
And, yes, students have complained that the code I gave them was not
working, that they got a strange error message and they promised that
they had typed exactly what I had written on the lab sheet. On
checking, they had, of course, typed "plot(fm = lm(y ~ . , somedata))"
Cheers,
Berwin
More information about the R-help
mailing list