[R] write.fortran
Dear all, 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=))) Best regards, baptiste sessionInfo() R version 2.10.1 RC (2009-12-06 r50690) i386-apple-darwin9.8.0 locale: [1] en_GB.UTF-8/en_GB.UTF-8/C/C/en_GB.UTF-8/en_GB.UTF-8 attached base packages: [1] tools stats graphics grDevices utils datasets methods base other attached packages: [1] foreign_0.8-38 __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] write.fortran
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. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] write.fortran
baptiste auguie-5 wrote: 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 Oops. Missing sep= and closing }. The correct line is for(k in seq(1,nrow(m))){cat(c(sprintf(%7d,m[k,1]),sprintf(%16.7e,m[k,2:8]),\n),sep=)} Berend -- View this message in context: http://n4.nabble.com/write-fortran-tp1587119p1587272.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] write.fortran
Thanks, it is indeed a bit cleaner. I was hoping for a more generic solution but I guess people use cat() and sprintf() in such cases. Thanks, baptiste On 10 March 2010 12:46, Berend Hasselman b...@xs4all.nl wrote: baptiste auguie-5 wrote: 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 Oops. Missing sep= and closing }. The correct line is for(k in seq(1,nrow(m))){cat(c(sprintf(%7d,m[k,1]),sprintf(%16.7e,m[k,2:8]),\n),sep=)} Berend -- View this message in context: http://n4.nabble.com/write-fortran-tp1587119p1587272.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] write.fortran
Try this. It takes a matrix or data.frame, codes= which is a vector of % codes and other text and file= and other cat arguments. If no ... args are given it outputs the character string that it would have handed to cat. write.mat - function(mat, codes, sep = , ...) { s - do.call(sprintf, unname(c(paste(codes, collapse = ), as.data.frame(m if (length(list(...)) 0) cat(s, sep = sep, ...) else s } # test - m is from original post write.mat(m, rep(c(%i, %15.7e, \n), c(1, 6, 1)), file = ) On Wed, Mar 10, 2010 at 7:52 AM, baptiste auguie baptiste.aug...@googlemail.com wrote: Thanks, it is indeed a bit cleaner. I was hoping for a more generic solution but I guess people use cat() and sprintf() in such cases. Thanks, baptiste On 10 March 2010 12:46, Berend Hasselman b...@xs4all.nl wrote: baptiste auguie-5 wrote: 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 Oops. Missing sep= and closing }. The correct line is for(k in seq(1,nrow(m))){cat(c(sprintf(%7d,m[k,1]),sprintf(%16.7e,m[k,2:8]),\n),sep=)} Berend -- View this message in context: http://n4.nabble.com/write-fortran-tp1587119p1587272.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] write.fortran
That's perfect, thanks! baptiste On 10 March 2010 14:47, Gabor Grothendieck ggrothendi...@gmail.com wrote: Try this. It takes a matrix or data.frame, codes= which is a vector of % codes and other text and file= and other cat arguments. If no ... args are given it outputs the character string that it would have handed to cat. write.mat - function(mat, codes, sep = , ...) { s - do.call(sprintf, unname(c(paste(codes, collapse = ), as.data.frame(m if (length(list(...)) 0) cat(s, sep = sep, ...) else s } # test - m is from original post write.mat(m, rep(c(%i, %15.7e, \n), c(1, 6, 1)), file = ) On Wed, Mar 10, 2010 at 7:52 AM, baptiste auguie baptiste.aug...@googlemail.com wrote: Thanks, it is indeed a bit cleaner. I was hoping for a more generic solution but I guess people use cat() and sprintf() in such cases. Thanks, baptiste On 10 March 2010 12:46, Berend Hasselman b...@xs4all.nl wrote: baptiste auguie-5 wrote: 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 Oops. Missing sep= and closing }. The correct line is for(k in seq(1,nrow(m))){cat(c(sprintf(%7d,m[k,1]),sprintf(%16.7e,m[k,2:8]),\n),sep=)} Berend -- View this message in context: http://n4.nabble.com/write-fortran-tp1587119p1587272.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.