[R] name scoping within dataframe index

Duncan Murdoch murdoch at stats.uwo.ca
Mon Jan 26 20:43:05 CET 2009

On 1/26/2009 2:20 PM, Alexy Khrabrov wrote:
> On Jan 26, 2009, at 2:12 PM, Duncan Murdoch wrote:
>>>> df[get("colname", parent.frame()) == value,]
>>> Actually, what I propose is  a special search rule which simply  
>>> looks  at the enclosing dataframe.name[...] outside the brackets  
>>> and looks up  the columns first.
>> Yes, I understood that, and I explained why it would be a bad idea.
> Well this is the case in all programming languages with scoping where  
> inner-scope variables override the outer ones.  Usually it's solved  
> with prefixing with the outer scope, outercsope.name or  
> outerscope::name or so.  So it only underscores the need to improve  
> scoping access in R.
> Dataframe column names belong to the dataframe object and the natural  
> thing would be to enable easy access to naming; you'd need to apply an  
> extra effort to access an overridden unrelated external variable.   
> Again, just an analogy from other programming languages.

The issue is that in most cases the outer scope would be unnamed:  it's 
the one that currently doesn't need a prefix.  So if we have a prefix 
meaning "this scope", why wouldn't that evaluate to "df" in that 
context?  I guess we need a prefix meaning "the caller's scope", but 
that's just going to lead to confusion:  is it the caller of the 
function that is trying to index df, or the function trying to do the 
indexing?  So we'd need a prefix specific to indexing:  and that's just 
too ugly for words.

As I said, use subset() or with().  For subset selection, subset() works 
very nicely.  (I don't like the way it does column selection, but that's 
a different argument.)

Duncan Murdoch

More information about the R-help mailing list