3

I am trying to import a text file containing latitudes and longitudes into R, which all have different degrees of precision (i.e. points after the decimal). When I try to use read.table, the data imports but cuts off at 5 decimal places. Is there a way around this? I have not found anything in the documentation that worked.

An example of data from the text file:

35.326354762000001 91.761496755
35.512443542 92.356571172
35.319078183999999 92.233513991
35.315654623 91.912230315
35.293864976999998 92.442997238
35.435569418 92.231860894

Thanks.

erasmortg
  • 3,246
  • 1
  • 17
  • 34
user41569
  • 31
  • 1
  • 3

3 Answers3

5

read.table doesn't round (unless you provide more than ~16 digits behind the comma), but indeed the precision to which the answer is printed may be controlled with options(digits=[n])

  • Ah! Got it, thank you! Is there a way to change the formatting to display more digits by default? I haven't found one (in RStudio) – user41569 Jul 13 '15 at 13:45
  • You could add a line in your .Rprofile file –  Jun 26 '17 at 11:38
0

You could try options(digits(), like so:

options(digits=5)
35.326354762000001
#[1] 35.326
options(digits=20)
35.326354762000001
#[1] 35.326354762000001
erasmortg
  • 3,246
  • 1
  • 17
  • 34
0

As pointed our by @Mark, it's just a display issue. The read.table is treating the 2nd column as a numeric. You can force read it as a char as follows to display all digits while printing the data frame. Say you have the data in the file test.txt... The following will generate the desired output

df <- read.table("test.txt", sep=" ",colClasses = c("character", "character"))