[R] is there an option to "turn off" scientific notation in write.csv

William Dunlap wdunlap at tibco.com
Fri Oct 14 19:53:19 CEST 2011


Have you tried setting
   options(scipen=500) # big number of digits
?  E.g.,

> df <- data.frame(x=pi*10^seq(-30,30,by=10), d=seq(-30,30,by=10), s=state.name[31:37])
> getOption("scipen")
[1] 0
> write.csv(df, stdout())
"","x","d","s"
"1",3.14159265358979e-30,-30,"New Mexico"
"2",3.14159265358979e-20,-20,"New York"
"3",3.14159265358979e-10,-10,"North Carolina"
"4",3.14159265358979,0,"North Dakota"
"5",31415926535.8979,10,"Ohio"
"6",3.14159265358979e+20,20,"Oklahoma"
"7",3.14159265358979e+30,30,"Oregon"
> oldScipen <- options(scipen=500)
> write.csv(df, stdout())
"","x","d","s"
"1",0.00000000000000000000000000000314159265358979,-30,"New Mexico"
"2",0.0000000000000000000314159265358979,-20,"New York"
"3",0.000000000314159265358979,-10,"North Carolina"
"4",3.14159265358979,0,"North Dakota"
"5",31415926535.8979,10,"Ohio"
"6",314159265358979334144,20,"Oklahoma"
"7",3141592653589793216422042866402,30,"Oregon"
> options(oldScipen) # reset to the previous value

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Chris Conner
> Sent: Friday, October 14, 2011 10:31 AM
> To: Paul Hiemstra
> Cc: r-help at r-project.org
> Subject: Re: [R] is there an option to "turn off" scientific notation in write.csv
> 
> Paul,
> 
> Many thanks for your help! I tried the suggestions below and, unfortunately, they didn't work for
> me.  I actually tried a tweak even:
> 
> Here is what I tried (might I be missing something?):
> 
> df$x <-format(df$x, scientific = FALSE)
> write.csv(df ,file="df.csv")
> df$x <-format(df$x, scientific = FALSE)
> write.csv(format(df, scientific = FALSE) ,file="df.csv")
> 
> For some reason write.csv still wants to coerce the long numeric into scientific notation prior to
> writing the .csv file...
> 
> I'm running 2.13.1 on a Win 7 machine... I wonder if this is a bug?
> 
> One work around I've figured out is to paste a character to df$x before exporting, then trimming the
> character after export... but there has to be a more elegant solution?
> 
> 
> ________________________________
> From: Paul Hiemstra <paul.hiemstra at knmi.nl>
> 
> Cc: "r-help at r-project.org" <r-help at r-project.org>
> Sent: Friday, October 14, 2011 12:39 AM
> Subject: Re: [R] is there an option to "turn off" scientific notation in write.csv
> 
> 
> 
> On 10/14/2011 05:25 AM, Chris Conner wrote:
> Dear Help-Rs,
> 
> I'm working with a file that contains large numbers and I need to export them "as is".  for example
> take:
>   x <- c(27104010002005,27104020001805,27104090001810,90050013000140,90050013000120)
> y <- c(1:5)
> df <- data.frame(cbind(x,y))
> 
> When I then try a simple:
> write.csv(df,file="df.csv")
> 
> I get:
>  x y
> 1 2.7104E+13 1
> 2 2.7104E+13 2
> 3 2.71041E+13 3
> 4 9.005E+13 4
> 5 9.005E+13 5
> 
> Then I tried:
> options(scipen=999)
> df$x <- as.character(df$x)
> Hi,
> 
> You can use format in this case:
> 
> df$x <-format(df$x, scientific = FALSE)
> write.csv(df,file="df.csv")
> 
> regards,
> Paul
> 
> 
> 
> write.csv(df,file="df.csv")
> 
> and I still get:
>  x y
> 1 2.7104E+13 1
> 2 2.7104E+13 2
> 3 2.71041E+13 3
> 4 9.005E+13 4
> 5 9.005E+13 5
> 
> How can I have R write the file so it looks like this:
>  x y
> 1 27104010002005
> 2 27104020001805
> 3 27104090001810
> 4 90050013000140
> 5 90050013000120 [[alternative HTML version deleted]]
> >
> ______________________________________________ R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-
> project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 
> 
> --
> Paul Hiemstra, Ph.D.
> Global Climate Division
> Royal Netherlands Meteorological Institute (KNMI)
> Wilhelminalaan 10 | 3732 GK | De Bilt | Kamer B 3.39
> P.O. Box 201 | 3730 AE | De Bilt
> tel: +31 30 2206 494 http://intamap.geo.uu.nl/~paul
> http://nl.linkedin.com/pub/paul-hiemstra/20/30b/770
> 	[[alternative HTML version deleted]]



More information about the R-help mailing list