[R] writing to a fixed format (fortran) file

jim holtman jholtman at gmail.com
Sat Aug 27 22:10:10 CEST 2005


One way of creating fixed width output is to use 'sprintf' to create
the string and write the resulting data out.  I used your input and
just selected columns 7-11 as an example.  You will have to supply
whatever field width you want.  I create a matrix and null out the row
and column names and then write the matrix out without quotes.

HTH

> x.1
   V1 V2 V3   V4   V5   V6   V7   V8   V9 V10 V11 V12   V13
1   1  1  1 19.5 2.42 0.02 5.81  9.7  0.4 102 4.8 320   4.8
2   2  1  1  0.0 0.00 0.00 0.00  4.7 -4.0 178 5.4 301   0.2
3   3  1  1  8.2 1.64 0.08 6.93  6.9 -3.6 275 2.7  84 -11.1
4   4  1  1  0.0 0.00 0.00 0.00 20.6 -4.8 221 5.6 327 -10.4
5   5  1  1  0.0 0.00 0.00 0.00 11.6  8.2 168 4.3 269   6.8
6   6  1  1  0.0 0.00 0.00 0.00 18.7 16.9 155 5.6 287   8.2
7   7  1  1  0.0 0.00 0.00 0.00  7.0  2.1 195 2.7  22   0.1
8   8  1  1  0.0 0.00 0.00 0.00 17.6  6.5 281 2.0 146   1.5
9   9  1  1 41.2 1.54 0.82 6.96 12.2  7.8 268 5.5 356   4.5
10 10  1  1  0.0 0.00 0.00 0.00 14.6 -1.4 250 3.6 344   6.4
11 11  1  1  0.0 0.00 0.00 0.00 14.5 -3.7 300 0.0   0 -16.9
12 12  1  1  0.0 0.00 0.00 0.00  8.8 -2.6 308 0.0   0   2.9
13 13  1  1  0.0 0.00 0.00 0.00  6.4  1.6 226 3.3 335   3.8
# field widths are 6,6,8,5,7.  You also control the number of decimals
that appear
> x.2 <- sprintf("%6.2f%6.1f%8.1f%5.0f%7.1f",x.1[,7], x.1[,8], x.1[,9],
+     x.1[,10], x.1[,11])
> x.2 <- as.matrix(x.2)  # convert to a character matrix
> dimnames(x.2) <- list(rep('', nrow(x.2)), '')  # blank row and column names
> noquote(x.2)
                                 
   5.81   9.7     0.4  102    4.8
   0.00   4.7    -4.0  178    5.4
   6.93   6.9    -3.6  275    2.7
   0.00  20.6    -4.8  221    5.6
   0.00  11.6     8.2  168    4.3
   0.00  18.7    16.9  155    5.6
   0.00   7.0     2.1  195    2.7
   0.00  17.6     6.5  281    2.0
   6.96  12.2     7.8  268    5.5
   0.00  14.6    -1.4  250    3.6
   0.00  14.5    -3.7  300    0.0
   0.00   8.8    -2.6  308    0.0
   0.00   6.4     1.6  226    3.3
> 


On 8/27/05, Jean Eid <jeaneid at chass.utoronto.ca> wrote:
> why not write.table with sep="\t"
> 
> On Sat, 27 Aug 2005, Duncan Golicher wrote:
> 
> > Could anyone help with what should be a simple task? I have data as a
> > fixed format (fortran) table. I  have no trouble getting it into R using
> > read.table. Each column is separated by a space, including the first
> > column that begins with a space, and aligned. It reads into R as if
> > separated by tabs. However I want to manipulate two columns of data then
> > write the results out into exactly the same fortran format for use in
> > another program.  It should be simple, but I've tried a variety of
> > experiments with print, cat and format, none of which have come close.
> >
> > Here is a sample of the data.
> >
> >   1  1    1  19.5  2.42 0.02   5.81   9.7   0.4 102.  4.8  320.   4.8
> >   2  1    1   0.0  0.00 0.00   0.00   4.7  -4.0 178.  5.4  301.   0.2
> >   3  1    1   8.2  1.64 0.08   6.93   6.9  -3.6 275.  2.7   84. -11.1
> >   4  1    1   0.0  0.00 0.00   0.00  20.6  -4.8 221.  5.6  327. -10.4
> >   5  1    1   0.0  0.00 0.00   0.00  11.6   8.2 168.  4.3  269.   6.8
> >   6  1    1   0.0  0.00 0.00   0.00  18.7  16.9 155.  5.6  287.   8.2
> >   7  1    1   0.0  0.00 0.00   0.00   7.0   2.1 195.  2.7   22.   0.1
> >   8  1    1   0.0  0.00 0.00   0.00  17.6   6.5 281.  2.0  146.   1.5
> >   9  1    1  41.2  1.54 0.82   6.96  12.2   7.8 268.  5.5  356.   4.5
> >  10  1    1   0.0  0.00 0.00   0.00  14.6  -1.4 250.  3.6  344.   6.4
> >  11  1    1   0.0  0.00 0.00   0.00  14.5  -3.7 300.  0.0    0. -16.9
> >  12  1    1   0.0  0.00 0.00   0.00   8.8  -2.6 308.  0.0    0.   2.9
> >  13  1    1   0.0  0.00 0.00   0.00   6.4   1.6 226.  3.3  335.   3.8
> >
> > --
> >
> > Dr Duncan Golicher
> > Ecologia y Sistematica Terrestre
> > Conservación de la Biodiversidad
> > El Colegio de la Frontera Sur
> > San Cristobal de Las Casas,
> > Chiapas, Mexico
> >
> > Email: dgoliche at sclc.ecosur.mx
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
> >
> 
> 
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
> 
> 


-- 
Jim Holtman
Convergys
+1 513 723 2929

What the problem you are trying to solve?




More information about the R-help mailing list