[Rd] Bug in subsetting data frame (PR#13515)

Duncan Murdoch murdoch at stats.uwo.ca
Tue Feb 10 14:31:47 CET 2009


Stavros Macrakis wrote:
> Don't know if this is the problem, but....
>
> 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.  Sometimes this causes trouble (e.g. if you passed data.frame to 
apply or do.call), but usually not, and I don't think it would cause 
trouble below.  What we see below is a simple misunderstanding of the 
difference between [] and $.

Duncan Murdoch


>         -s
>
> On 2/10/09, xinlee883 at stat.math.ethz.ch <xinlee883 at stat.math.ethz.ch> wrote:
>   
>> Full_Name: Xin Lee
>> Version: 2.8.0
>> OS: Windows XP
>> Submission from: (NULL) (193.200.150.23)
>>
>>
>> Dear developer
>>
>> I discover annoying bug in subsetting data frame.  Here is reproducable
>> examples:
>>
>>     
>>> data.frame <- data.frame(x = runif(5), y = runif(5), row.names =
>>>       
>> c('a','b','c','d','e')
>>
>>     
>>> subset <- data.frame['x']
>>> subset['a',]
>>>       
>> work
>>
>>     
>>> subset <- data.frame$x
>>> subset['a',]
>>>       
>> not work
>>
>>     
>>> subset <- data.frame['a',]
>>> subset[,'x']
>>>       
>> work
>>
>>     
>>> subset <- data.frame[,'x']
>>> subset['a',]
>>>       
>> not work
>>
>> I hope this is easy fix for you and works corectly soon.
>>
>> Sincerely,
>> Xin Lee
>>
>> ______________________________________________
>> R-devel at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>>     
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>



More information about the R-devel mailing list