[R] Output Data Formatting Question

jim holtman jholtman at gmail.com
Fri Mar 13 22:39:41 CET 2009


I don't know why sprintf does not work for you.  Here is something
close; you will have to work on the spacing but the formating in
sprintf lets you specify the field size:

> sprintf("%2s %-20s%6d%14.5f", "7", "WORD", 5550, 3.14158)
[1] " 7 WORD                  5550       3.14158"
>


On Fri, Mar 13, 2009 at 4:48 PM, Jason Rupert <jasonkrupert at yahoo.com> wrote:
>
> Within R I need to reformat my data so that the output looks like the following:
> " 7 WORDS              5550         3.14159         -6.           6."
>
> Previous folks were doing this by hand, but I would like to have a formula do it.
>
>
> That is,
> (a) 7 (place holder) on second column, then a space,
> (b) WORDS (placeholder) is a string descriptor that is left justified to column 4,
> (c) 5550 (place hoder) is right justified to column 26,
> (d) 3.14159 (place holder) is right justified to column 41,
> (e) -6. (place holder) is right justified to column 53, and
> (f) 6. (place holder) is right justified to column 66.
>
> The basic sprintf commands did not seem like they would get me there, nor did the "paste" capability.
>
> After getting the data into that format I plan to use "write" to place the data in an output *.txt file.
>
> Thank you for any hints and tips that can be provided.
>
> ______________________________________________
> 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
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?




More information about the R-help mailing list