I'm using readr::write_csv()
to export a data.frame in the .csv format to another system. By doing that I noticed that write_csv()
increases the number of some decimals, which is a problem for the other application.
You can reproduce this behavior with the following code:
options(digits = 15)
x = structure(list(rsds = c(1.40066760539752, 1.62027378202433, -1.44847242961156,
1.69761995098528, -0.942939230819493, -0.068529066008811, 1.76822135039912,
-1.20101547762003, -0.829135203700728, 1.26695938729229, -2.01720249708251,
1.81301280008168), co2 = c(-0.773101574330299, -0.773101574330298,
-0.773101574330298, -0.773101574330298, -0.773101574330298, -0.773101574330298,
-0.773101574330298, -0.773101574330298, -0.773101574330298, -0.773101574330298,
-0.773101574330297, -0.773101574330298)), row.names = c(NA, -12L
), class = "data.frame")
y <- round(x,7)
y
library(readr)
write_csv(y, "y.csv")
read.csv2("y.csv")
the output:
### Rounded output
> y
rsds co2
1 1.4006676 -0.7731016
2 1.6202738 -0.7731016
3 -1.4484724 -0.7731016
4 1.6976200 -0.7731016
5 -0.9429392 -0.7731016
6 -0.0685291 -0.7731016
7 1.7682214 -0.7731016
8 -1.2010155 -0.7731016
9 -0.8291352 -0.7731016
10 1.2669594 -0.7731016
11 -2.0172025 -0.7731016
12 1.8130128 -0.7731016
### Numbers tempered by write_csv()
> read.csv2("y.csv")
rsds.co2
1 1.4006676, -1.6202738
2 1.6202738,-0.77310160000000006
3 -1.4484724,-0.77310160000000006
4 1.69762,-0.77310160000000006
5 -0.9429392, -1.7682214
6 -0.0685291,-0.77310160000000006
7 1.7682214,-0.77310160000000006
8 -1.2010155, -0.9429392
9 -0.8291352,-0.77310160000000006
10 1.2669594, -1.4006676
11 -2.0172025, -1.2669594
12 1.8130128,-0.77310160000000006
As you can see, some numbers were kept round while others gained additional decimals after being saved in .csv (in this example just one number, but I saw the same thing happening with other numbers in my original dataset). Is that an inherent flaw of write_csv() or is there a fix for it inside the tidyverse?