Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Number format, writing 1e-5 instead of 0.00001

Tags:

format

numbers

r

I've used read.table to read a file that contains numbers such as 0.00001

when I write them back with write.table those numbers appear as 1e-5

How can I keep the old format?

like image 570
skan Avatar asked Sep 06 '25 14:09

skan


2 Answers

I would just change the scipen option before calling write.table. Note that this will also change how numbers are displayed when printing to the console.

options(scipen=10)
write.table(foo, "foo.txt")
options(scipen=0)  # restore the default
like image 66
Joshua Ulrich Avatar answered Sep 08 '25 10:09

Joshua Ulrich


You can do this by converting your numbers to strings with formatting as you require, then using the argument quote = FALSE in the call to write.table.

dfr <- data.frame(x = 10^(0:15))
dfr$y <- format(dfr$x, scientific = FALSE)
write.table(dfr, file = "test.txt", quote = FALSE)

Note that you shouldn't need to change the format of the numbers in your file. Pretty much every piece of scientific software and every spreadsheet understands scientific notation for numbers, and also has number formatting options so you can view them how you choose.

like image 36
Richie Cotton Avatar answered Sep 08 '25 10:09

Richie Cotton