[R] Formatted output with alternating format at different rows

jim holtman jholtman at gmail.com
Mon Jan 3 21:40:25 CET 2011


'sprintf' if your friend:

> dummy3 = c(1.1, 2.2, 3.3)
> dummy4 = c(4.4, 5.5, 6.6, 7.7)
> dummy2 = c(8.8, 9.9)
>
> cat(sprintf("%5.1f%6.2f%7.3f\n", dummy3[1], dummy3[2], dummy3[3]))
  1.1  2.20  3.300
> cat(sprintf("%5.2f%6.3f%7.4f%8.5f\n", dummy4[1], dummy4[2], dummy4[3], dummy4[4]))
 4.40 5.500 6.6000 7.70000
> cat(sprintf("%5.3f%6.4f\n", dummy2[1], dummy2[2]))
8.8009.9000
>
>


On Mon, Jan 3, 2011 at 10:38 AM, Ray Laymon <ray.laymon064 at gmail.com> wrote:
> Dear all,
>
> I have a simple question. I couldn't find a solution in the
> forums/R-user manual; I have also asked to my friends who use R, but
> couldn't get any answer from them either. I would appreciate any
> solutions.
>
> I want to write formatted text file like in Fortran. More specifically
> with the format choice of mine for any given line (more specifics are
> given below).
>
> The R function read.fortran() read files like in Fortran, but I
> couldn't find any function to write files in a similar way..
>
> Since I want different formats for different lines, write(),
> write.table(), write.fwf() functions didn't work for me.
>
> Thanks for your time
>
> Ray
>
>
>
>
> ##########
> ##########
> In Fortran
> ##########
>
> real dummy3(3)
> real dummy4(4)
> real dummy2(2)
>
> dummy3 = (/1.1, 2.2, 3.3/)
> dummy4 = (/4.4, 5.5, 6.6, 7.7/)
> dummy2 = (/8.8, 9.9/)
>
> OPEN(UNIT=320,FILE='output.dat',FORM='FORMATTED')
> write(320,501) dummy3
> write(320,502) dummy4
> write(320,503) dummy2
> close(320)
>
> 501 format(F5.1,F6.2,F7.3)
> 502 format(F5.2,F6.3,F7.4,F8.5)
> 503 format(F5.3,F6.4)
> ##########
> ##########
>
> This should produce an output something like below (I used * instead
> of the spaces)
>
> **1.1**2.20***3.300
> *4.40*5.500**6.6000*7.70000
> 8.8009.9000
>
> And a function in [R] that will enable me to write something like
> above is what I am looking for..
> ##########
> ##########
> ##########
>
> ______________________________________________
> 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.
>



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?



More information about the R-help mailing list