[Rd] Bug in subsetting data frame (PR#13515)
Duncan Murdoch
murdoch at stats.uwo.ca
Tue Feb 10 16:11:58 CET 2009
Stavros Macrakis wrote:
> On Tue, Feb 10, 2009 at 8:31 AM, Duncan Murdoch <murdoch at stats.uwo.ca>wrote:
>
>
>> Stavros Macrakis wrote:
>>
>>
>>> It is a bad idea to set data.frame <- xxx since R has a single
>>> namespace for functions and variables.
>>>
>>>
>>>
>> That's not quite accurate. R mixes functions and variables in namespaces,
>> but it has lots of namespaces, and it usually doesn't cause much trouble to
>> have a variable named data.frame in one, and a function named data.frame in
>> another. The evaluator recognizes the context of usage and will get the
>> function for a function call. If you retrieve data.frame without doing a
>> function call, you'll get whichever one it finds first, which is typically
>> the one in the global environment, as below.
>>
>>
>
> Interesting. I was not aware of this rule.
>
> Can you point me to chapter and verse in the language definition (
> http://cran.r-project.org/doc/manuals/R-lang.html)? I couldn't find it, but
> I probably looked in the wrong place.
>
No, I'm also unable to find it.
> | Sometimes this causes trouble (e.g. if you passed data.frame to apply or
> do.call), but usually not, ...
>
> Indeed! It is quite surprising that functions are defined using ordinary
> assignment, but function lookup is different from normal variable
> evaluation, e.g. that after c<-4, c(10) is different from (c)(10). Was this
> inspired by some other language? I don't think it's done this way in any
> other language I can think of....
I think originally there was no difference, and it caused the obvious
trouble when people used variable names like t and c other short
function names, so this was added. I don't remember whether the
different lookup rules showed up first in R or S.
Duncan Murdoch
More information about the R-devel
mailing list