Save matrix in CSV file to R without format loss

I am trying to write a matrix to a CSV file using write.matrix from MASS, but I am having some problems. When I print the matrix, it looks something like this.

pqs S2 R2 R2adj Cp AIC PRESS 1 0 1 167.27779 27981.8583 NA NA 3679.294476 NA NA 2 1 2 160.32254 25703.3165 0.08866209 0.08142925 3343.909110 1666.993 3338167.3 3 1 2 86.73559 7523.0630 0.73326195 0.73114498 891.016823 1509.726 1045980.3 4 1 2 67.50458 4556.8690 0.83843145 0.83714916 490.815893 1445.555 693993.5 

but when I do this write.matrix(moDat2, file = paste(targetPath, "dat2.csv", sep="/"), sep=",")

It will save it in a file like this

 p,q,s,S2,R2,R2adj,Cp,AIC,PRESS 0.000000e+00,1.000000e+00,1.672778e+02,2.798186e+04, NA, NA,3.679294e+03, NA, NA 1.000000e+00,2.000000e+00,1.603225e+02,2.570332e+04,8.866209e-02,8.142925e-02,3.343909e+03,1.666993e+03,3.338167e+06 1.000000e+00,2.000000e+00,8.673559e+01,7.523063e+03,7.332620e-01,7.311450e-01,8.910168e+02,1.509726e+03,1.045980e+06 

In any case, can I save it in a file without converting the data to scientific notation?

+4
source share
2 answers

You can use format inside your write.matrix call.

  write.matrix(format(moDat2, scientific=FALSE), file = paste(targetPath, "dat2.csv", sep="/"), sep=",") 
+5
source

Help (the page for MASS :: write.table does not imply any available controls. This is the write.table help page about formating numbers:

"In almost all cases, the conversion of numerical values ​​is determined by the" scipen "option (see parameters), but with an internal equivalent of digits = 15. For finer control, use the format to create a character matrix / data frame and call write.table on this."

0
source

All Articles