0

I want to import a table (.txt file) in R with read.table().

table1<- read.table("input.txt",sep = "\t")

The file contains data like 0.09165395632583884

After reading the data, data becomes 0.09165396. Last few digits are lost, but I want to avoid this problem.

If I used

options(digits=22)

then it creates another problem, like maindata = 0.19285969062961023 but when I write the data in file,

write.table(table1,file = "output.txt",col.names = F, row.names = F)

I get data = 0.192859690629610225. Here, last digit is extra and the second last digit is change.

Can someone give me a hint how to solve the problem?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Animesh Kumar Paul
  • 2,241
  • 4
  • 26
  • 37
  • 1
    Are you sure it's truncating, and not just not printing them? `print(d , digits = 16)` – alistaire Jun 19 '16 at 14:32
  • It is not duplicate. I need this solution . I modify the question. – Animesh Kumar Paul Jun 19 '16 at 14:57
  • 1
    Welcome to floating point representation. [The links here](http://stackoverflow.com/questions/9508518/why-are-these-numbers-not-equal/9508558#9508558) explain what's happening. The simplest (if hacky) solution is just to store your numbers as strings. – alistaire Jun 19 '16 at 15:20
  • Although, this [post](http://stackoverflow.com/questions/37694066/export-to-csv-and-import-with-same-numerical-precision/37694705#37694705) is about csv, it should help as the idea is the same. – Christoph Jun 20 '16 at 14:20

0 Answers0