vroom_write writes negative zeros to file

49 views Asked by At

I am trying to use vroom::vroom_write to write a tibble to a text file. Within my R session, I see that the third column of my tibble has some zeros. When I examine the text file, I see that some of zeros are written as negative zero.

Here is example code to reproduce what I see.

library(magrittr)
set.seed(2022-06-07)
dat <- tibble::tibble(rand = rnorm(n = 10, mean = 0, sd = 1), indicator = rbinom(n = 10, size = 1, prob = 0.5), product = rand * indicator)
dat %>%
  vroom::vroom_write(file = "out.txt")

Here is dat in my R session.

> dat
# A tibble: 10 × 3
     rand indicator product
    <dbl>     <int>   <dbl>
 1 -0.851         1  -0.851
 2  0.274         1   0.274
 3 -0.179         1  -0.179
 4 -0.257         0   0    
 5  1.33          0   0    
 6 -1.85          0   0    
 7  0.191         1   0.191
 8  0.441         0   0    
 9 -1.46          0   0    
10 -0.754         0   0    
> 

Here is the file out.txt.

rand    indicator       product
-0.8514459712815987     1       -0.8514459712815987
0.2738400972346478      1       0.2738400972346478
-0.17889364131136923    1       -0.17889364131136923
-0.25728795761508944    0       -0
1.3288498659722705      0       0
-1.847720986710459      0       -0
0.19140525644162554     1       0.19140525644162554
0.44121526714638787     0       0
-1.4602030403350934     0       -0
-0.7538551785550484     0       -0

Is this a known problem for vroom? Is there a solution? I'd like to avoid having negative zeros in my file out.txt.

Here is my R session info.

devtools::session_info()
─ Session info ───────────────────────────────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.2.0 (2022-04-22)
 os       Ubuntu 18.04.5 LTS
 system   x86_64, linux-gnu
 ui       RStudio
 language (EN)
 collate  en_US.UTF-8
 ctype    en_US.UTF-8
 tz       America/Detroit
 date     2022-06-07
 rstudio  2022.02.2+485 Prairie Trillium (server)
 pandoc   2.17.1.1 @ /usr/lib/rstudio-server/bin/quarto/bin/ (via rmarkdown)

─ Packages ───────────────────────────────────────────────────────────────────────────────────────────
 package        * version    date (UTC) lib source
 assertthat       0.2.1      2019-03-21 [3] CRAN (R 4.0.3)
 bit              4.0.4      2020-08-04 [3] CRAN (R 4.0.3)
 bit64            4.0.5      2020-08-30 [3] CRAN (R 4.0.3)
 brio             1.1.3      2021-11-30 [1] CRAN (R 4.1.2)
 cachem           1.0.6      2021-08-19 [3] CRAN (R 4.1.1)
 callr            3.7.0      2021-04-20 [3] CRAN (R 4.0.5)
 cli              3.3.0      2022-04-25 [1] CRAN (R 4.2.0)
 colorspace       2.0-3      2022-02-21 [3] CRAN (R 4.2.0)
 crayon           1.5.1      2022-03-26 [1] CRAN (R 4.2.0)
 data.table       1.14.2     2021-09-27 [1] CRAN (R 4.1.2)
 DBI              1.1.2      2021-12-20 [3] CRAN (R 4.2.0)
 desc             1.4.1      2022-03-06 [3] CRAN (R 4.2.0)
 devtools         2.4.3      2021-11-30 [1] CRAN (R 4.1.2)
 digest           0.6.29     2021-12-01 [1] CRAN (R 4.1.2)
 distributional   0.3.0      2022-01-05 [1] CRAN (R 4.2.0)
 dplyr            1.0.9      2022-04-28 [1] CRAN (R 4.2.0)
 ellipsis         0.3.2      2021-04-29 [3] CRAN (R 4.0.5)
 evaluate         0.15       2022-02-18 [3] CRAN (R 4.2.0)
 fansi            1.0.3      2022-03-24 [3] CRAN (R 4.2.0)
 farver           2.1.0      2021-02-28 [3] CRAN (R 4.0.5)
 fastmap          1.1.0      2021-01-25 [3] CRAN (R 4.0.3)
 fs               1.5.2      2021-12-08 [1] CRAN (R 4.2.0)
 generics         0.1.2      2022-01-31 [3] CRAN (R 4.2.0)
 ggdist           3.1.1      2022-02-27 [1] CRAN (R 4.2.0)
 ggplot2          3.3.6      2022-05-03 [3] CRAN (R 4.2.0)
 glue             1.6.2      2022-02-24 [1] CRAN (R 4.2.0)
 gtable           0.3.0      2019-03-25 [3] CRAN (R 4.0.3)
 htmltools        0.5.2      2021-08-25 [3] CRAN (R 4.1.1)
 htmlwidgets      1.5.4      2021-09-08 [3] CRAN (R 4.1.1)
 jmuOutlier       2.2        2019-08-05 [1] CRAN (R 4.2.0)
 knitr            1.39       2022-04-26 [3] CRAN (R 4.2.0)
 lifecycle        1.0.1      2021-09-24 [3] CRAN (R 4.1.1)
 magrittr       * 2.0.3      2022-03-30 [1] CRAN (R 4.2.0)
 memoise          2.0.1      2021-11-26 [1] CRAN (R 4.1.2)
 munsell          0.5.0      2018-06-12 [3] CRAN (R 4.0.3)
 pillar           1.7.0      2022-02-01 [3] CRAN (R 4.2.0)
 pkgbuild         1.3.1      2021-12-20 [1] CRAN (R 4.2.0)
 pkgconfig        2.0.3      2019-09-22 [3] CRAN (R 4.0.3)
 pkgload          1.2.4      2021-11-30 [1] CRAN (R 4.1.2)
 prettyunits      1.1.1      2020-01-24 [3] CRAN (R 4.0.3)
 processx         3.5.3      2022-03-25 [3] CRAN (R 4.2.0)
 ps               1.7.0      2022-04-23 [3] CRAN (R 4.2.0)
 purrr            0.3.4      2020-04-17 [3] CRAN (R 4.0.3)
 R6               2.5.1      2021-08-19 [3] CRAN (R 4.1.1)
 remotes          2.4.2      2021-11-30 [1] CRAN (R 4.1.2)
 rlang            1.0.2      2022-03-04 [1] CRAN (R 4.2.0)
 rmarkdown        2.14       2022-04-25 [1] CRAN (R 4.2.0)
 rprojroot        2.0.3      2022-04-02 [3] CRAN (R 4.2.0)
 rstudioapi       0.13       2020-11-12 [3] CRAN (R 4.0.3)
 scales           1.2.0      2022-04-13 [3] CRAN (R 4.2.0)
 sessioninfo      1.2.2      2021-12-06 [1] CRAN (R 4.1.2)
 testthat         3.1.4      2022-04-26 [1] CRAN (R 4.2.0)
 tibble           3.1.7      2022-05-03 [1] CRAN (R 4.2.0)
 tidyselect       1.1.2      2022-02-21 [3] CRAN (R 4.2.0)
 tzdb             0.3.0      2022-03-28 [3] CRAN (R 4.2.0)
 usethis          2.1.6      2022-05-25 [1] CRAN (R 4.2.0)
 utf8             1.2.2      2021-07-24 [3] CRAN (R 4.1.1)
 vctrs            0.4.1      2022-04-13 [1] CRAN (R 4.2.0)
 vroom            1.5.7.9000 2022-05-22 [1] Github (r-lib/vroom@2218ba0)
 withr            2.5.0      2022-03-03 [1] CRAN (R 4.2.0)
 xfun             0.31       2022-05-10 [1] CRAN (R 4.2.0)
 yaml             2.3.5      2022-02-21 [3] CRAN (R 4.2.0)

 [1] /net/mulan/home/fredboe/R/x86_64-pc-linux-gnu-library/4.0
 [2] /usr/local/lib/R/site-library
 [3] /net/mario/cluster/lib/R/site-library-bionic-40
 [4] /usr/lib/R/site-library
 [5] /usr/lib/R/library

──────────────────────────────────────────────────────────────────────────────────────────────────────
> 
1

There are 1 answers

1
langtang On BEST ANSWER

This is likely a precision issue; before you write the file, you might try to replace values that are "near" 0 with 0, using near()

dat <- dat %>% mutate(product = if_else(near(product,0),0,product))