Skip to content
Prev 349746 / 398506 Next

Verify that a grid is uniform

The aim is to control if a given abscissa/grid is uniform or not. Abscissa
in generic vector of real ordered numbers.

Here a reproducibile code:

# uniform abscissa/grid
abscissa1 <- seq(0, 1, length=100)
# non-uniform abscissa/grid
abscissa2 <- sort(runif(100))

control1 <- all(signif(abscissa1[1:(length(abscissa1) - 1) + 1] -
abscissa1[1:(length(abscissa1) - 1)]) == signif(rep((range(abscissa1)[2] -
range(abscissa1)[1])/(length(abscissa1) - 1), length(abscissa1) - 1)))
control2 <- all(signif(abscissa2[1:(length(abscissa2) - 1) + 1] -
abscissa2[1:(length(abscissa2) - 1)]) == signif(rep((range(abscissa2)[2] -
range(abscissa2)[1])/(length(abscissa2) - 1), length(abscissa2) - 1)))

control1
control2

As expected control1 is TRUE and control2 is FALSE. Actually in this code
it is possible also to use
diff inside signif.
Do you mean that the control to perform can be done in this manner

if (length(unique(diff(vec))) == 1) {
  control <- TRUE
} else {
  control <- FALSE
}

I have tried to apply this control on abscissa1 which is uniform but
length(unique(diff(abscissa1))) was greater than one; probably, as you
said, this is due to the fact that in this way I don't take into account
the machine precision.
What I want to understand is if there is a SAFE solution, even if until now
this control is working correctly. I have seen in the documentation of
signif that by default the number of digits considered are 6. The number of
digits to consider depends on the scale used. It doesn't make sense to
increase the number of digits with respect to default because, in this
case, you are not using an handy scale.
Maybe it could be better directly to ask user if the abscissa passed as
argument is uniform or not.
Thanks a lot for the link!!!

Marc




2015-04-06 16:32 GMT+02:00 Sarah Goslee <sarah.goslee at gmail.com>: