[R] name scoping within dataframe index
Duncan Murdoch
murdoch at stats.uwo.ca
Mon Jan 26 20:12:43 CET 2009
On 1/26/2009 2:01 PM, Alexy Khrabrov wrote:
>> On 1/26/2009 1:46 PM, Alexy Khrabrov wrote:
>>> Every time I have to prefix a dataframe column inside the indexing
>>> brackets with the dataframe name, e.g.
>>> df[df$colname==value,]
>>> -- I am wondering, why isn't there an R scoping rule that search
>>> starts with the dataframe names, as if we'd said
>>> with(df, df[colname==value,])
>>> -- wouldn't that be a reasonable default to prepend to the name
>>> search path?
>>
>> If you did that, it would be quite difficult to get at a "colname"
>> variable that *isn't* the column of df. It would be something like
>>
>> 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.
Duncan Murdoch
>
> It would break legacy code which used the column names identical to
> variables in this context, but there's probably other ideas to enhance
> R readability which would break legacy code. Perhaps when the next
> major overhaul occurs, this is something folks can voice opinions
> about. I find the need for inner prefixing quite unnatural, FWIW.
>
> Cheers,
> Alexy
More information about the R-help
mailing list