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

Berwin A Turlach berwin at maths.uwa.edu.au
Wed Dec 7 02:57:26 CET 2005


>>>>> "vic" == vincent  <vincent at 7d4.com> writes:

    vic> ronggui a $A(&(Bcrit :
    >> 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

    vic> x is an argument local to mean(), did you expect another
    vic> answer ?

    >>> mean(x<-1:10)
    >> [1] 5.5
    >>> x
    >> [1] 1 2 3 4 5 6 7 8 9 10

    vic> What is the goal of this "example" ?
I believe to demonstrate that "<-" is preferable.

    vic> Here with "<-", (voluntary, or not, side effect) the global
    vic> variable x is, also, created.
Who says that the variable x is global?  The example above looks as if
it was typed on the command line and, hence, x would be global.  But
the same piece of code could be inside a function with x being a
variable local to that function.

    vic> Did the writer really want that ???
Yes.  Perhaps a more realistic example is the following:
> library(MASS)
> par(mfrow=c(2,2))
> plot( fm <- lm(log(brain) ~ log(body), mammals) )

A construct I use frequently to fit a linear model and obtain the
diagnostic plots at the same time.  And I had students appearing in my
office complaining that the code in the lab sheet produced errors and
swearing that they typed in exactly what I have written on the lab
sheet.  But, of course, they had substituted "=" for "<-"

    vic> I though there were other specific statements especially
    vic> intended for global assignment, eg "<<-".
As you say, these are for global assignments.  Nobody said that in
rongui's example "x" or in my example "fm" is a global variable.  They
could be local to a function.

Cheers,

        Berwin

========================== Full address ============================
Berwin A Turlach                      Tel.: +61 (8) 6488 3338 (secr)   
School of Mathematics and Statistics        +61 (8) 6488 3383 (self)      
The University of Western Australia   FAX : +61 (8) 6488 1028
35 Stirling Highway                   
Crawley WA 6009                e-mail: berwin at maths.uwa.edu.au
Australia                        http://www.maths.uwa.edu.au/~berwin




More information about the R-help mailing list