1

I am trying to export a data.frame which has 2 columns having 16 and 24 long integers.

However after exporting to csv, I am getting scientific notation like 4.352370e+15 in place of original non-scientific integer.

Code

write.csv(fulljoin,file="output2.csv")
Rich Scriven
  • 97,041
  • 11
  • 181
  • 245
LonelySoul
  • 1,212
  • 5
  • 18
  • 45
  • Give some sample data and maybe someone will help out quicker. – N8TRO Oct 19 '14 at 03:12
  • 5
    See the Details section of `?write.csv`. It provides recommendations for how to handle this. – Joshua Ulrich Oct 19 '14 at 03:15
  • It helps if you state the actual question eg. 'R write.csv - why am I getting unexpected high-values from data.frame export?'. Bonus points for mentioning the actual language you're using (I'm assuming it's R?). And some data always helps otherwise it's too difficult for people to test/answer. – Pete855217 Oct 19 '14 at 03:24
  • [great reproducible examples](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) are great – Rich Scriven Oct 19 '14 at 03:33
  • 3
    @Pete855217 it has an [tag:r] tag on it. It's generally accepted practice to [not use them in titles except on certain occasions](http://meta.stackexchange.com/questions/19190/should-questions-include-tags-in-their-titles) – hrbrmstr Oct 19 '14 at 05:02
  • @hrbrmstr fair enough re the R tag, although the rest of the comment still stands - the post was simply a statement. I'm assuming (guessing) it was "how can I avoid scientific notation in data.frame csv write output?". It's a useful question - scientific notation can be a PITA when handling the data further, and R can drive you a little mad when trying to do simple things like this. – Pete855217 Oct 29 '14 at 04:43

2 Answers2

1

Following option worked for me. Thanks to everyone for extending help.

options(digits=18)

Everything else was kept constant. It helped while reading as well as while writing.

LonelySoul
  • 1,212
  • 5
  • 18
  • 45
  • 2
    That is not a very general solution. Try `options(digits=18); x <- 123456789012345678901234; write.csv(x, "longnumber.csv")` and you will still get scientific notation in your output. – C8H10N4O2 Oct 20 '14 at 17:13
  • @JohnAndrews . Very true and unfortunately not even a decent solution. Although for my purpose which was very urgent it has proven useful. – LonelySoul Oct 20 '14 at 18:36
0

I would not count on getting that many digits without loss of precision in the default integer data type. Notice that the integers will actually get coerced to numeric after a certain length. Compare class(12345678L) (integer) to class(123456789012L) (numeric with warning). After a little more length you will start to lose precision, regardless of how many digits you are displaying:

option(digits=22) # the max
x <-  1234567890123456789012; x
# [1] 1234567890123456774144 -- whoops!

For larger integers you may want to use a different class such as Big Integer in gmp.

library(gmp)
x <- as.bigz("1234567890123456789012345678901234567890")
x <- x + 1 # do some math
write.csv(as.character(x), "bignumber.csv", row.names=FALSE, quote=FALSE) 
# csv looks like:
# x
# 1234567890123456789012345678901234567891
C8H10N4O2
  • 18,312
  • 8
  • 98
  • 134