[R] writing a matrix to a file with trailing zeroes

Erin Hodgess erinm.hodgess at gmail.com
Sun Jul 17 17:33:58 CEST 2016


Hello everyone!

I'm looking at this problem, and I'm sure there is a straightforward
solution.  I have a matrix and some of the values have zeroes to the right
of the decimal point.  When I write these to a file, the zeroes disappear.
The material below is what I have tried so far.



> x2
              x          y
[1,] -1.3611521  5.0000000
[2,] -0.8521120 -1.0512976
[3,] -0.9652820 -0.1306795
[4,] -0.4319187  1.2483199
[5,] -0.7548402  1.3404867
[6,]  6.0000000  2.5000000

>write.table(sprintf("%.1f",t(x2)),row.name=FALSE,col.name=FALSE,file="")
"-1.4"
"5.0"
"-0.9"
"-1.1"
"-1.0"
"-0.1"
"-0.4"
"1.2"
"-0.8"
"1.3"
"6.0"
"2.5"
> write(sprintf("%.1f",t(x2)),file="")
-1.4
5.0
-0.9
-1.1
-1.0
-0.1
-0.4
1.2
-0.8
1.3
6.0
2.5
> write(round(t(x2),1),file="")
-1.4 5 -0.9 -1.1 -1
-0.1 -0.4 1.2 -0.8 1.3
6 2.5
> write(round(t(x2),1),file="",ncol=2)
-1.4 5
-0.9 -1.1
-1 -0.1
-0.4 1.2
-0.8 1.3
6 2.5
>


Still no luck.  I need the 5 in the first line to be 5.0 and the 6 in the
last line to be 6.0.

Any suggestions, please?

Thanks,
Erin

-- 
Erin Hodgess
Associate Professor
Department of Mathematical and Statistics
University of Houston - Downtown
mailto: erinm.hodgess at gmail.com

	[[alternative HTML version deleted]]



More information about the R-help mailing list