[R] write.fortran

Gabor Grothendieck ggrothendieck at gmail.com
Wed Mar 10 14:47:15 CET 2010


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.auguie at 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 <bhh at 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 at 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 at 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.
>



More information about the R-help mailing list