Skip to content

vec_unique() makes a distinction between +0 and -0 while unique() does not #637

Description

@cmuyehara

Hi,

I noticed that vec_unique() seems to make a distinction between +0 and -0 while most R functions do not. This can affect the behavior of pivot_wider when +0 and -0 are present in the id_cols, causing it to generate two rows instead of one.

This seems to be something about how the underlying C function vctrs_unique_loc() works, but that's as far as I got.

library(tidyverse)
library(vctrs)

posZero = 0
negZero = ceiling(-0.9)

unique(posZero, negZero)
#> [1] 0
vec_unique(c(posZero, negZero ))
#> [1] 0 0

# using `abs()` to convert -0 to a +0 fixes the problem
vec_unique(abs(c(posZero, negZero)))
#> [1] 0

# Similarly, `identical()` normally treats these as equivalent
identical(posZero, negZero, num.eq = T)
#> [1] TRUE

# However, setting num.eq = F does a bitwise comparison and differentiates between +0 and -0
identical(posZero, negZero, num.eq = F)
#> [1] FALSE

# This can cause `pivot_wider()` to generate extra rows
data.frame(id = c(posZero, negZero),
           names = c('a', 'b'), 
           vals = c(1,2)) %>%
  pivot_wider(id_cols = id, names_from =  names, values_from = vals )
#> # A tibble: 2 x 3
#>      id     a     b
#>   <dbl> <dbl> <dbl>
#> 1     0     1    NA
#> 2     0    NA     2

# using `abs()` to convert the -0 to a +0 again fixes the problem
data.frame(id = c(posZero, negZero),
           names = c('a', 'b'), 
           vals = c(1,2)) %>%
  mutate(id = abs(id)) %>%
  pivot_wider(id_cols = id, names_from =  names, values_from = vals )
#> # A tibble: 1 x 3
#>      id     a     b
#>   <dbl> <dbl> <dbl>
#> 1     0     1     2

Created on 2019-10-24 by the reprex package (v0.3.0)

Activity

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