[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 03:39:56 CET 2010

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:

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

Thanks for any insight.  Bryan
Bryan Hanson
Acting Chair
Professor of Chemistry & Biochemistry
DePauw University, Greencastle IN USA

More information about the R-help mailing list