[R] Why doesn't vec[-real.number] give an error or warning? Kids do the darndest things!

Bryan Hanson hanson at depauw.edu
Sun Mar 14 15:09:57 CET 2010


Jim, I completely agree with everything you said.

I am curious about the cases where one would use a real number as an index
and anticipate that R would do the as.integer for you, rather than turning
it into an integer yourself.

If anyone has a simple example or situation where this arises, I'd like to
see it.  Of course, most cases might be rather idiosyncratic, and too
complex to really share.  Just wondering mostly, because I am not a
programmer by trade, and when I write code that uses indexing, and the
proposed index comes up as a real number, I've generally made a mistake in
my thinking!

Bryan


On 3/13/10 11:39 PM, "jim holtman" <jholtman at gmail.com> wrote:

> That notation  'v[-1.18]' is not going to remove the 12th entry if it was
> 1.18.  They should have used:
>  
> v[-which(v == 1.18)]
>  
> to remove it, but it probably would not have worked and we would have refered
> them to FAQ 7.31.
>  
> R is doing exactly what you are asking it to do.  There are cases where I have
> used a real number and would not have expected an error. 
>  
> As you say, kids do the darnest things.  I use the comment that "users always
> find new and unusual ways to use a system that the developers never
> envisioned".  All we can do it tell them how it does work (for better or for
> worse) and even if they think it is wrong, we can not change it as this point
> in time to adapt to their way that they think it should work.
>  
> R is a great learning experience for people and it allows you to experiment
> without breaking things.  I still have to write little test cases every so
> often to understand how things work, especially if you have NAs in your data
> and you are trying to do some equality test or do a calculation on it.
> 
> On Sat, Mar 13, 2010 at 9:39 PM, Bryan Hanson <hanson at depauw.edu> wrote:
>> Hi all...
>> 
>> My students were conflating grepping for a value in a vector to get the
>> index, and then removing it with [-index], for instance like this:
>> 
>> set.seed(17)
>> v <- rnorm(20)
>> s <- v[-1.18]
>> 
>> They were trying to remove the 12th value in v, which is -1.18 or so.
>> 
>> But the result is, as documented in ?Extract, to coerce 1.18 to the next
>> lowest integer, and remove the 1st value of v, not the 12th value.
>> 
>> This strikes me as something that ought to give an error or warning, or at
>> least be in the R Inferno, but I couldn't find it there.
>> 
>> Under what circumstances would the documented behavior be valuable?  I can
>> see that it avoids doing floor or as.integer in computations where an index
>> is needed but real numbers are generated for whatever reason, but is that
>> common?
>> 
>> Thanks for any insight.  Bryan
>> *************
>> Bryan Hanson
>> Acting Chair
>> Professor of Chemistry & Biochemistry
>> DePauw University, Greencastle IN USA
>> 
>> ______________________________________________
>> 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.
> 
> 



More information about the R-help mailing list