0

When writing out a zoo object to a CSV file using write.zoo(), I would actually like to drop decimal precision from the default of 14 to just 3. However, even with setting scipen & digits in options(), I have not been able to drop the precision in output.

Here is a sample code to illustrate the issue.

blah <- zoo(cbind(c(1.590833333333335, NA), c(NA, 21.590833333333337)))
index(blah) <- c("Dec 1985", "Dec 1986")
colnames(blah) <- c("FooHeader", "BarHeader")
options(scipen = 3, digits = 3)
write.zoo(blah, file = "blah.csv", sep = ",")

If I open the blah.csv file, I see

"Index","FooHeader","BarHeader"
"Dec 1985",1.59083333333334,NA
"Dec 1986",NA,21.5908333333333

But what I would really want to see is

"Index","FooHeader","BarHeader"
"Dec 1985",1.591,NA
"Dec 1986",NA,21.591

How do I go about making this happen? Thanks a bunch in advance!

Note: I am aware that by dropping the precision, if I read the data into the R again, I would still lose the precision. That's fine. I can live with that.

Antony
  • 5,414
  • 7
  • 27
  • 32

3 Answers3

6

Try this

write.zoo(round(blah, 3), sep = ",")
## "Index","FooHeader","BarHeader"
## "Dec 1985",1.591,NA
## "Dec 1986",NA,21.591
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
  • This answer needs to rise to the top. In case anyone doesn't know, G. Grothendieck is the hands down master of zoo methods. (The reason I put as.character was to get the requested quotes.) – IRTFM Nov 02 '12 at 23:27
2

?write.table states that:

In almost all cases the conversion of numeric quantities is governed by the option "scipen" (see options), but with the internal equivalent of digits=15. For finer control, use format to make a character matrix/data frame, and call write.table on that.

Unfortunately format outputs a matrix so a bit of tidying needs to be done to get it back to a zoo object:

write.zoo(`index<-`(zoo(format(blah,digits=4)),index(blah)))
"Index" "FooHeader" "BarHeader"
"Dec 1985" " 1.591" "    NA"
"Dec 1986" "    NA" "21.591"
James
  • 65,548
  • 14
  • 155
  • 193
  • Thank you James. It is a good start, but two additional issues come up. 1. Format seems to make everything into a string, which should not be the case. Even with Trim option, I am stilling seeing double quotes around numbers. 2. In actual dataset, with format, I end up with numbers with scientific notations, i.e. "1.591e+01". I haven't figured out how to get rid of e+01. The drop0trailing does not solve the issue either. – Antony Nov 02 '12 at 15:44
  • @Antony, just add `quote=FALSE`. `write.zoo(`index<-`(zoo(format(blah,digits=4)),index(blah)), quote=FALSE)` – GSee Nov 02 '12 at 16:40
2

I would have used coredata<- and round to coerce the numeric values to the right precision and since you want them quoted use as.character after that.

 coredata(blah) <- as.character(round(coredata(blah), 3))
 write.zoo(blah, file="testzoo.csv", sep= ",")
#----file------
"Index","FooHeader","BarHeader"
"Dec 1985","1.591",NA
"Dec 1986",NA,"21.591"
IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • Thanks a bunch. This is great! It helps me get rid of trailing scientific notations too! Also, I didn't need the as.character. The index is from the zoo object, and the coredata really does not include the index column. But good to know about as.character – Antony Nov 02 '12 at 15:50