advice/opinion on "<-" vs "=" in teaching R
On 15-Jan-10 09:29:16, Deepayan Sarkar wrote:
On Fri, Jan 15, 2010 at 2:38 PM, Ted Harding <Ted.Harding at manchester.ac.uk> wrote:
On 15-Jan-10 08:14:04, Barry Rowlingson wrote:
On Fri, Jan 15, 2010 at 6:57 AM, Ted Harding <Ted.Harding at manchester.ac.uk>wrote:
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.
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? ?_Remember: 'eschew obfuscation'. Barry
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").
For example:
x = xyplot(1~1) system.time(x = xyplot(1~1))
Error in system.time(x = xyplot(1 ~ 1)) : unused argument(s) (x = xyplot(1 ~ 1))
system.time({ 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
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 ------------------------------