[Rd] negative numerics in []

Henrik Bengtsson hb at biostat.ucsf.edu
Thu Sep 4 18:52:20 CEST 2014


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).

/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



More information about the R-devel mailing list