[R] customize the step value

Duncan Murdoch murdoch@dunc@n @end|ng |rom gm@||@com
Fri Oct 29 15:07:31 CEST 2021


On 29/10/2021 4:34 a.m., PIKAL Petr wrote:
> Hi
> 
> One has to be careful when using fractions in seq step.
> 
> Although it works for 0.5
>> (seq(0,10, .5) - round(seq(0,10,.5),2))==0
>   [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
> TRUE
> [16] TRUE TRUE TRUE TRUE TRUE TRUE
> 
> in case of 0.3 (or others) it does not always result in expected values (see
> FAQ 7.31 for explanation)
> 
>> (seq(0,10, .3) - round(seq(0,10,.3),2))==0
>   [1]  TRUE  TRUE  TRUE FALSE  TRUE  TRUE FALSE  TRUE  TRUE FALSE  TRUE  TRUE
> [13] FALSE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE FALSE
> [25] FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE


Petr is right, it's unsafe to use fractional values for the step.  0.5 
works because it has a power of 2 in the denominator and so does the 
start value, but it's easy to make mistakes when you rely on that (e.g. 
changing the step size from 0.5 to 0.3 would break things).

A better idea is to modify a sequence of integers.  For example, to get 
1.5 to 3.5 by 0.5, you can do (3:7)*0.5, and for 0 to 3 by 0.3, use 
(0:10)*0.3.

Duncan Murdoch



More information about the R-help mailing list