Skip to content

advice/opinion on "<-" vs "=" in teaching R

17 messages · Erin Hodgess, Rolf Turner, (Ted Harding) +10 more

#
Hi R People:

I'm teaching a statistical computing class using R starting next week
(yay!) and I have an opinion type question, please.

I'm old school and use "<-" in an assignment.

However, I'm starting to see the "=" in the literature.

Which should I use or does it matter, please?

Thanks for your input!
Sincerely,
Erin
#
On 15/01/2010, at 4:45 PM, Erin Hodgess wrote:

            
It's basically a matter of taste.  But people with ***good*** taste  
use "<-". :-)

Constructions such as "a = a+3" are toadally illogical whereas "a <- a 
+3"
makes perfect sense.

However I'm sore afraid that as is always the case, good taste fails and
bad taste prevails.  I.e. "=" for assignment will take over.  Personally
I shall resist as long as possible, i.e. until "<-" is removed from the
syntax structure by R Core.

	cheers,

		Rolf

######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}
#
On 15-Jan-10 04:06:55, Rolf Turner wrote:
Yes, water flows down-hill -- unless we build control structures ...
There is at least one context where the distinction must be
preserved. Example:

  pnorm(1.5)
  # [1] 0.9331928
  pnorm(x=1.5)
  # Error in pnorm(x = 1.5) : unused argument(s) (x = 1.5)
  pnorm(x<-1.5)
  # [1] 0.9331928
  x
  # [1] 1.5

Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 15-Jan-10                                       Time: 06:57:16
------------------------------ XFMail ------------------------------
#
On 15-Jan-10 08:14:04, Barry Rowlingson wrote:
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!

While I am at it, in addition to the above example, we can have

  x <- 1.234
  sqrt(x=4)
  # [1] 2
  x
  # [1] 1.234

compared with (as in the first example):

  x <- 1.234
  sqrt(x<-4)
  # [1] 2
  x
  # [1] 4

There is a passage in ?"<-" (which I don't completely understand)
which is also relevant to Erin's query about '=' vs '<-':

  The operators '<-' and '=' assign into the environment in
  which they are evaluated.  The operator '<-' can be used
  anywhere, whereas the operator '=' is only allowed at the
  top level (e.g., in the complete expression typed at the
  command prompt) or as one of the subexpressions in a braced
  list of expressions.

(I'm not too clear about the scope of "one of the subexpressions
in a braced list of expressions").

Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 15-Jan-10                                       Time: 09:08:41
------------------------------ XFMail ------------------------------
#
On Fri, Jan 15, 2010 at 2:38 PM, Ted Harding
<Ted.Harding at manchester.ac.uk> wrote:
For example:
Error in system.time(x = xyplot(1 ~ 1)) :
  unused argument(s) (x = xyplot(1 ~ 1))
user  system elapsed
  0.008   0.000   0.005

Of course, <- would not have had a problem. This is the most common
problem I personally have had using = for assignment (better
readability of <- is also a huge plus).

-Deepayan
#
On 15-Jan-10 09:29:16, Deepayan Sarkar wrote:
Thanks for spelling out "braced list", Deepayan.
And I have to agree with you about the readability of "<-".

Indeed, as summary advice to Erin, I wouild say use "<-" except
in the specific context of assigning values to named parameters
in functions, to named elements of lists, and the like (where
use of "=" is mandatory anyway, or you won't get the desired
effect -- see below).

  list(x<-1.234,y<-2.345)
  # [[1]]
  # [1] 1.234
  # [[2]]
  # [1] 2.345

  list(x=1.234,y=2.345)
  # $x
  # [1] 1.234
  # $y
  # [1] 2.345

Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 15-Jan-10                                       Time: 10:01:55
------------------------------ XFMail ------------------------------
#
G'day all,

On Fri, 15 Jan 2010 09:08:44 -0000 (GMT)
(Ted Harding) <Ted.Harding at manchester.ac.uk> wrote:

            
Depending on the definition of foo() more than two lines might be
necessary to clarify what this code is actually supposed to do. :)
Absolutely.
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
#
Barry Rowlingson wrote:
I do like their experimental design:

"In a bizarre event which one of the authors insists was planned, and
the other maintains was a really stupid idea that just happened to work,
the test was ?rst administered to about 30 students on a...."

("Like" in the sense of being amused, of course)

-p
#
Barry Rowlingson wrote:
The most common use I see that I like is within a conditional test like

if (  !is.null(x <- get("x", somehow)) && length(x) == 1) { dosomething }

The x variable is only used for the test, but since it is used twice 
there, the assignment saves getting it twice.  You could expand it to 
two lines

x <- get("x", somehow)
if ( !is.null(x) && length(x) == 1) { dosomething }

but I find that a tiny bit harder to read. 

On the other hand, I would never use the examples you gave, because I'd 
have no idea what the value of x would be, since it depends on the order 
of evaluation of the arguments.  In R, I don't even know for sure if the 
assignment would be evaluated at all, let alone before the x argument.

Duncan Murdoch
#
I've only been using R for about 2.5 years but and I'm not all that good  but I vote for <- .

I think the deciding factor is in  RSiteSearch() and the various manuals.

Almost everything I see uses <- .  Why introduce = when it is not used normally?  It will just confuse the students who are trying to use any of the documentation.  

Not to mention they might slammed for bad syntax on the R-help mailing list.  :)
--- On Thu, 1/14/10, Erin Hodgess <erinm.hodgess at gmail.com> wrote:

            
__________________________________________________________________
The new Internet Explorer? 8 - Faster, safer, easier.  Optimized for Yahoo!  Get it Now for Free! at http://downloads.yahoo.com/ca/internetexplorer/
#
John Kane <jrkrideau <at> yahoo.ca> writes:
I vote for <- .
normally?  It will just confuse the
Those are all good reasons.
  I have said something similar before
(see <http://www.mail-archive.com/r-help at r-project.org/msg16904.html>),
but I tend to use = because it seems to be more intuitive for 
students, despite being logically confused at a deeper level, 
and I want to spare them any additional cognitive load when they 
are first getting introduced to R.
   I'm not particularly convinced by the "<- is more general
and there are some contexts where = doesn't work", because I'm
not trying to be absolutely rigorous, nor teach all the possible
ins and outs of R syntax. I would be very surprised if any of
the examples given actually came up in the course of a first-semester
statistics/modeling R course. I just want to do what works best for
the students -- the problem is deciding on the balance between
short term benefit (<- is one more odd thing to get used to)
and long term benefit (they will see <- in other contexts, so
they might as well get used to it eventually).

  Ben Bolker
#
On Fri, 15 Jan 2010, Barry Rowlingson wrote:

            
It allows the lazy evaluation mechanism to determine whether the  assignment happens. That would be very hard to do in two lines of code.

         -thomas

Thomas Lumley			Assoc. Professor, Biostatistics
tlumley at u.washington.edu	University of Washington, Seattle
#
On Thu, 14 Jan 2010, Erin Hodgess wrote:

            
I would say to use = if you are teaching people familiar with C or Java, and to use <- otherwise.

       -thomas

Thomas Lumley			Assoc. Professor, Biostatistics
tlumley at u.washington.edu	University of Washington, Seattle
#
On Fri, Jan 15, 2010 at 10:00 AM, Ben Bolker <bolker at ufl.edu> wrote:
I teach the idiom

summary(fm1 <- lm(y ~ x, mydata))

in my introductory courses.
1 day later
#
On 01/15/2010 10:08 PM, Peter Dalgaard wrote:
This is, of course, entirely consistent with the concept of outliers as 
potentially patentable discoveries that was discussed only a few days ago...

Jim