[R] write.fortran
Berend Hasselman
bhh at xs4all.nl
Wed Mar 10 12:41:07 CET 2010
baptiste auguie-5 wrote:
>
> I'm trying to write tabular data to a text file, these data will be
> the input of a Fortran program. The format needs to be
> "(i7,2x,7(e15.7,2x))". I have not been able to find a clean way of
> producing this output with write.table. I searched for a
> "write.fortran" function similar to read.fortran() in package utils
> but I couldn't find any. Below is a small example of what I'm trying
> to achieve, but it's clearly suboptimal in many ways,
>
> m <- cbind(seq(1, 5), matrix(rnorm(7*5), ncol=7))
>
> do.call(cat, c(lapply(seq(1, nrow(m)), function(ii){
> x <- m[ii, ]
> sprintf("%i %15.7e %15.7e %15.7e %15.7e %15.7e %15.7e %15.7e
> \n", ii , x[1],x[2],x[3],x[4],x[5],x[6],x[7])
> }), list(sep="")))
>
How about this (found after a lot fiddling and all sorts of error
messages:-))
do.call(cat,c(lapply(seq(1,nrow(m)),function(k)
{c(sprintf("%7d",m[k,1]),sprintf("%16.7e",m[k,2:8]),"\n")}),list(sep="")))
This is a lot easier
for(k in
seq(1,nrow(m))){cat(c(sprintf("%7d",m[k,1]),sprintf("%16.7e",m[k,2:8]),"\n"))
Berend
--
View this message in context: http://n4.nabble.com/write-fortran-tp1587119p1587268.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list