Hello,
Às 14:07 de 29/10/21, Duncan Murdoch escreveu:
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.
But even this is not safe.
Perhaps the most frequent sequence causing problems (R-Help questions)
is the one from 0 to 1 by 0.1 and why it always returns FALSE when
tested against certain values such as 0.6.
And it also fails to create it with (0:10)*0.1.
x <- seq(0, 1, by = 0.1)
any(x == 0.6)
#[1] FALSE
y <- (0:10)*0.1
any(y == 0.6)
#[1] FALSE
all(x == y)
#[1] TRUE
Off-topic: FAQ 7.31 addresses this.
Hope this helps,
Rui Barradas
Duncan Murdoch
______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.