[R] RE: stange behavior of subset [] (was: lowess + turnpoints = doubling integers?)
Philippe Grosjean
phgrosjean at sciviews.org
Fri Jan 3 15:04:03 CET 2003
Tom Blackwell wrote:
>...
>I summarized this to myself as "computed subscripts need explicit
>rounding in R, but not in S". Here's the sample code which gave
>me different results with R than with Splus. I no longer have
>Splus available, so I can't check it again.
>look <- (10 * seq(14)) - 76
>chk.1 <- seq(1420)[ 10 * (73.1 + look) ] # NOT what I expected
>chk.2 <- seq(1420)[ round(10 * (73.1 + look), 0) ] # much better.
>...
Hum, hum,... It reminds me a "bug" that was very difficult to track in a
function! This is a very interesting point. Actually, it works exactly the
same in Splus 6.1 for Windows and in R 1.6.1. Look also at the following:
> look <- (10 * seq(14)) - 76
> 10 * (73.1 + look)
[1] 71 171 271 371 491 586 681 791 886 981 1101 1201 1301 1401
> as.integer(10 * (73.1 + look))
[1] 70 170 270 370 490 586 681 791 886 981 1101 1201 1301 1401
as.integer() does not round doubles, but truncates them toward zero... and
[] coerces doubles to integers using as.integer(). This is not a bug, since
it is fully documented in Splus:
In help("["):
...
The expressions may also be logical, numeric, or character. Numeric
subscripts should be integers, such as the output from : (the sequence
operator).
...
Subscripting coerces non-integer numeric subscripts to integers using
as.integer . Because as.integer creates integers by truncating the numeric
representation, this coercion can lead to unexpected results.
and in help(as.integer):
...
The numbers are truncated (moved to the closest integer the original number
that is towards zero). Attributes are deleted.
However, this could be vicious! Why not to round double in as.integer()?
Perhaps for performance questions?
Yet, this is not documented at all in R (neither in help("["), nor in
help(as.integer)!!!). Any other comment on this?
Best,
Philippe Grosjean
...........]<(({°<...............<°}))><...............................
) ) ) ) )
( ( ( ( ( Dr. Philippe Grosjean
) ) ) ) )
( ( ( ( ( LOV, UMR 7093
) ) ) ) ) Station Zoologique
( ( ( ( ( Observatoire Océanologique
) ) ) ) ) BP 28
( ( ( ( ( 06234 Villefranche sur mer cedex
) ) ) ) ) France
( ( ( ( (
) ) ) ) ) tel: +33.4.93.76.38.16, fax: +33.4.93.76.38.34
( ( ( ( (
) ) ) ) ) e-mail: phgrosjean at sciviews.org
( ( ( ( ( SciViews project coordinator (http://www.sciviews.org)
) ) ) ) )
.......................................................................
More information about the R-help
mailing list