Phdaml Phdaml - 3 months ago 15
R Question

Scientific notation last digits omitted as zeros when write.csv

I have a lot of long numbers and r reads them as scientific notation. But when I

write.csv
, the scientific notation becomes an incorrect number with a bunch of zeros following. For example,
3.894e+13
will become
38944400000000
after the
write.csv.


I have exact numbers in the place where the zeros are.
How do I keep the exact number when exporting a data file?

[update]:
(1) The problem is because when I save as csv in excel, it loses digits of long numbers. It is an excel bug and I use excel 2016.

(2) when the above problem occurred, I tired to set
options(scipen=999)
. When I summarize the data, the summary statistics are omitted always in this file. I tried other files, it (
summary
) works without losing precision. When I do print the numbers, it is correct, only the summary statistics are omitted after I set options.

Answer

Set the the scipen option to be a large enough number before writing the csv file is one way to make it work:

df = data.frame(x = 1232939143546532)
options(scipen = 30)
write.csv(df, "test.cv")

This gives the following:

"","x"
"1",1232939143546532