[R] Inexplicably different results using subset vs bracket notation on logical variable

David Winsemius dwinsemius at comcast.net
Wed Aug 29 06:12:29 CEST 2012


On Aug 28, 2012, at 11:52 AM, Mauricio Cornejo wrote:

> Peter,
>
> You're right ... and I do think I've finally understood the help  
> file on this.  I somehow missed the connection between my variable's  
> values, which include NAs, and the fact that those returned values  
> are in turn indices for '['.
>
> OK ... I'm all straight on this now.  (I lost count of how many  
> times I read the help file ... it'd be great if the help page could  
> be updated with a specific example on this sublety .... that's just  
> a thought).

I agree. An annotated example in the help page might help. I will post  
such a suggestion to r-devel. (I include myself in those who sometimes  
learn from example and counter-exampel better than we learn from  
theory.)

-- 
David.


>
> Thanks again to all for your patience with me on this,
> Mauricio
> New Jersey, USA
>
>
>
> ________________________________
> From: Peter Ehlers <ehlers at ucalgary.ca>
>
> Cc: William Dunlap <wdunlap at tibco.com>; David Winsemius <dwinsemius at comcast.net 
> >; peter dalgaard <pdalgd at gmail.com>; "r-help at r-project.org" <r-help at r-project.org 
> >
> Sent: Tuesday, August 28, 2012 2:08 PM
> Subject: Re: [R] Inexplicably different results using subset vs  
> bracket notation on logical variable
>
> On 2012-08-28 07:44, Mauricio Cornejo wrote:
>> William, David, and Peter,
>>
>>
>> Thank you all so much for your help on this.  Though I had read the  
>> help files on 'subset' and '[', I had not been able to discern from  
>> that text what the problem was.  I could not have solved it without  
>> your help.
>>
>>
>> The help file on 'subset' mentions "For ordinary vectors, the  
>> result is simply x[subset & !is.na(subset)]."  However, since I was  
>> dealing with a data frame at the time, I failed to appreciate the  
>> relevance (but I get it now).
>>
>>
>> I've gone back to read the help on '[' and I still don't see where  
>> this functionality is described.  The page has section titled "NAs  
>> in indexing", whose content I interpret as being limited to NAs in  
>> the indices (and not in the returned values).  I can't find any  
>> text in the documentation describing the subtle behavior you kindly  
>> pointed out.
>
> Hmmm, it seems to me that the page is clear:
>
>    "... NA index picks an unknown element and so *returns* NA
>     in the corresponding element of ..."
>
> (my emphasis)
>
> Isn't that exactly what occurs?
>
> Peter Ehlers
>
>>
>> Thanks again,
>> Mauricio
>>     [[alternative HTML version deleted]]
>>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius, MD
Alameda, CA, USA




More information about the R-help mailing list