[R] R is GNU S, not C.... [was "how to get or store ....."]

Hong Ooi Hong.Ooi at iag.com.au
Wed Dec 7 00:57:05 CET 2005


_______________________________________________________________________________________


Hm, a style war on R-Help. I wonder if this goes for long enough, we'll come to the question of where to put the braces. ;)

Personally, I always use <- instead of = where applicable. In addition to the distinction between assignment and naming arguments in function calls, it helps avoid the notorious =/== bug from C programming. Compare

    "if(x == 1) {..." /* comparison -- probably what was wanted */

with

    "if(x = 1) {..."  /* oops */

This has bitten me a number of times in C, so I'd like to avoid it in R/S. Using "if(identical(x, 1)) {..." also addresses this problem, but that just looks awkward to me.


-- 
Hong Ooi
Senior Research Analyst, IAG Limited
388 George St, Sydney NSW 2000
+61 (2) 9292 1566

-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Liaw, Andy
Sent: Wednesday, 7 December 2005 7:17 AM
To: 'vincent at 7d4.com'; r-help at stat.math.ethz.ch
Subject: Re: [R] R is GNU S, not C.... [was "how to get or store ....."]

From: vincent at 7d4.com
> 
> ronggui a écrit :
> 
> > I think it is NOT just for historical reason.
> > see the following example:
> > 
> >>rm(x)
> >>mean(x=1:10)
> > [1] 5.5
> >>x
> > Error: object "x" not found
> 
> x is an argument local to mean(),
> did you expect another answer ?
> 
> >>mean(x<-1:10)
> > [1] 5.5
> >>x
> >  [1]  1  2  3  4  5  6  7  8  9 10
> 
> What is the goal of this "example" ?

I believe it's to show why "<-" is to be preferred over "=" for
assignment...
 
> Here with "<-",
> (voluntary, or not, side effect)
> the global variable x is, also, created.
> Did the writer really want that ???

Very much so, I believe.
 
> I though there were other specific statements
> especially intended for global assignment, eg "<<-".

You need to distinguish assignment in function _call_ and assignment in
function _definition_.  They ain't the same.
 
> If this example was intended to prove "<-"
> is better than "="
> ... I'm not really convinced !

In that case, let's try another one (which is one big reason I stopped using
"=" for assignment):


> long.comp <- function(n) {
+     Sys.sleep(n)
+     n
+ }
> result = long.comp(30)
> system.time(result = long.comp(30))
Error in system.time(result = long.comp(30)) : 
	unused argument(s) (result ...)
> system.time(result <- long.comp(30))
[1]  0.00  0.00 30.05    NA    NA
> str(result)
 num 30

Cheers,
Andy
 
>

______________________________________________
R-help at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html



_______________________________________________________________________________________

The information transmitted in this message and its attachme...{{dropped}}




More information about the R-help mailing list