Skip to content

Strange behaviour comparing numeric vectors with NAs and small (perhaps floating point) differences #43

Closed

Description

@jameslairdsmith

I'm getting some strange behaviour when using waldo::compare() on numeric vectors which have both small differences (perhaps floating point) and NA values.

If I compare two normal vectors it shows the small difference between them.

library(waldo)

x <- seq(0.1, 0.4, 0.1)
y <- x
y[3] <- 0.300000001
compare(x, y)
#> `x`: 0.1000000000 0.2000000000 0.3000000000 0.4000000000
#> `y`: 0.1000000000 0.2000000000 0.3000000010 0.4000000000

If one of the vectors has an NA value, then that difference is shown, however the small numeric difference is not.

x[1] <- NA
compare(x, y)
#> `x`:  NA 0.2 0.3 0.4
#> `y`: 0.1 0.2 0.3 0.4

Finally if the two vectors both have an NA in the same position, an error is thrown.

y[1] <- NA
compare(x, y)
#> Error in sprintf(paste0("%0.", digits, "f"), x): invalid format '%0.NA'; use format %f, %e, %g or %a for numeric objects

Created on 2020-08-25 by the reprex package (v0.3.0)

Activity

  1. hadley commented on Aug 25, 2020

    @hadley
    Member

    Hmmm, solving this is going to require quite a lot of work — the problem is that you can only determine the optimal number of digits to display once you have aligned the diffs, and the code currently assumes that you're aligning strings (i.e. the numbers have already been formatted).

    Maybe there's some simpler heuristic that's still better than the current flawed approach, but it's not occurring to me right now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions