Dear R users; Is there a function in R that I can put "text" with proper alignments in a fixed format. For instance, if I have three fields: A, B and C, where both A and C are text with 3 characters and left alignment; B is a numeric one with 2 decimals and 3 integer space digits. How can I put the following row in a file? (note there is a space between a and 2, and after b.) (A=aaa, B=23.11 and C=bb) "aaa 23.11bb " Yours, Yihsu Chen
YIHSU CHEN wrote:> Dear R users; > > Is there a function in R that I can put "text" with proper alignments in > a fixed format. For instance, if I have three fields: A, B and C, where > both A and C are text with 3 characters and left alignment; B is a > numeric one with 2 decimals and 3 integer space digits. How can I put > the following row in a file? (note there is a space between a and 2, and > after b.) (A=aaa, B=23.11 and C=bb) > "aaa 23.11bb "Use sprintf: > A="aaa" > B=23.11 > C="bb" > sprintf("%s %.2f%s",A,B,C) [1] "aaa 23.11bb" Be sure to read the documentation for sprintf, as you'll want to fully understand left and right adjustment, and field width and precision. Best, Jeff -- http://biostat.mc.vanderbilt.edu/JeffreyHorner
YIHSU CHEN <yihsu.chen <at> ucmerced.edu> writes:> Dear R users; > > Is there a function in R that I can put "text" with proper alignments in > a fixed format. For instance, if I have three fields: A, B and C, where > both A and C are text with 3 characters and left alignment; B is a > numeric one with 2 decimals and 3 integer space digits. How can I put > the following row in a file? (note there is a space between a and 2, and > after b.) (A=aaa, B=23.11 and C=bb) > "aaa 23.11bb "Check write.fwf() in gdata package. It might help you, but you will have to create new column to have B and C together i.e. without any space between. Gregor