[Rd] negative numerics in []

Duncan Murdoch murdoch.duncan at gmail.com
Thu Sep 4 19:12:30 CEST 2014


On 04/09/2014 12:52 PM, Henrik Bengtsson wrote:
> I can reproduce this.  It seems to be happen when trying to drop the
> last element, e.g.
>
> > x <- 1:3
> > x[-3.1]
> [1] 1 2 3
> > x[-2.1]
> [1] 1 3
> > x[-1.1]
> [1] 2 3
>
> > x <- 1:2
> > x[-2.1]
> [1] 1 2
> > x[-1.1]
> [1] 2
>
> > x <- 1:4
> > x[-4.1]
> [1] 1 2 3
> > x[-3.1]
> [1] 1 2 4
> > x[-2.1]
> [1] 1 3 4
> > x[-1.1]
> [1] 2 3 4
>
> > x <- 1
> > x[-1.1]
> [1] 1
>
> My *guess* (all time I have) is that it's a bug where as.integer() is
> applied only *after* (silently) dropping negative indices out of
> range, e.g.
>
> > x <- 1:4
> > x[-c(1:10+0.1)]
> [1] 4
>
> Here -c(4:10+0.1) are dropped because they all > length(x).
>
> If someone wish to track this down further, the R source is available
> at https://svn.r-project.org/R/trunk/ (mirrored at
> https://github.com/wch/r-source).

The documentation in the R Language Definition states quite clearly that 
the truncation happens first, so this definitely looks like a bug.  It 
also states  "A negative out of bounds value for |i| [the index] causes 
an error", which is also not true, but it seems possible that a 
documentation change would be less disruptive than a code change.

Duncan Murdoch
>
> /Henrik
>
> On Thu, Sep 4, 2014 at 8:17 AM, Michael Haupt <michael.haupt at oracle.com> wrote:
> > Hello,
> >
> > I'm a bit puzzled by what looks (to me) like a discrepancy between documentation and implementation.
> >
> > The documentation for [] says this about the indices: "Numeric values are coerced to integer as by as.integer (and hence truncated towards zero)."
> >
> >> as.integer(-3.1)
> > [1] -3
> >
> > Good. But:
> >
> >> x <- c(1,2,3)
> >> x[-3.1]
> > [1] 1 2 3
> >
> > Given the documentation, I'd have expected a result of "[1] 1 2", because -3.1 should be coerced to -3 (by virtue of as.integer).
> >
> > What bit do I not get? (I'm using R 3.1.1, if that matters.)
> >
> > Best,
> >
> > Michael
> >
> > --
> > Dr. Michael Haupt
> > Principal Member of Technical Staff
> > Phone: +49 331 200 7277, Fax: +49 331 200 7561
> > Oracle Labs
> > Oracle Deutschland B.V. & Co. KG, Schiffbauergasse 14, 14467 Potsdam, Germany
> >
> > ______________________________________________
> > 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