简体   繁体   中英

How to use write.table when number of decimal places may exceed internal scipen option

Is it possible write the following vector without losing precision? The write.table documentation says that it has an internal equivalent of 'digits = 15' and it looks like scipen is being ignored.

a <- c(0.1111111111111123333, 0.11111111111111244, 0.1111111111111121111111)
options(scipen=22)
write.table(a, file="del.txt")

You can use the format function to control output before writing it to the table. For instance, the following code will format your vector output to 20 digits.

 write.table(format(a, digits=20), file="del.txt")

Edit : I've tried a few things, and can't seem to get R to not change the number. Here's what I've tried.

 print(c(0.1111111111111123333, 0.11111111111111244, 
         0.1111111111111121111111), digits=18)

[1] 0.111111111111112340 0.111111111111112437 0.111111111111112118

No good.

 a <- format(c(0.1111111111111123333, 0.11111111111111244, 
               0.1111111111111121111111), digits=19)

[1] "0.1111111111111123401" "0.1111111111111124372" "0.1111111111111121180"

No good.

 write.table(sprintf("%.100f", a), file='del.txt')

No good.

I'll keep looking for the answer, but hopefully somebody else will step in and clear it up.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM