[R] seq

Gabor Grothendieck ggrothendieck at myway.com
Fri Sep 3 17:20:10 CEST 2004


Henric Nilsson <henric.nilsson <at> statisticon.se> writes:

> 
> Hi everyone,
> 
> I've tried the below on R 1.9.1 and the 2004-08-30 builds of R 1.9.1 
> Patched and R 2.0.0 on Windows 2000, and the results are consistent.
> 
>  > seq(0.5, 0, by = -0.1)
> [1] 0.5 0.4 0.3 0.2 0.1 0.0
> 
>  > seq(0.7, 0, by = -0.1)
> [1]  7.000000e-01  6.000000e-01  5.000000e-01  4.000000e-01  3.000000e-01 
> 2.000000e-01  1.000000e-01 -1.110223e-16
> 
> Is this really the intended behaviour? I ended up using
> 
>  > seq(0.7, 0, length = 8)
> [1] 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0.0
> 
> which does what I want.
> 

You could also try (7:0)/10 .  The numerator will be an integer
sequence, which can be performed exactly, so you will have fewer
occasions for floating point representations and their
associated approximations:

   R> class(7:0)
   [1] "integer"
   R> (7:0)/10  # or seq(7,0)/10
   [1] 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0.0
   R> class((7:0)/10)
   [1] "numeric"




More information about the R-help mailing list