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

From: Berwin A Turlach <berwin_at_maths.uwa.edu.au>
Date: Wed 07 Dec 2005 - 12:57:26 EST

>>>>> "vic" == vincent <vincent@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


R-help@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 Received on Wed Dec 07 13:05:05 2005

This archive was generated by hypermail 2.1.8 : Wed 07 Dec 2005 - 14:30:41 EST