Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8130729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T08:49:48+00:00 2026-06-06T08:49:48+00:00

Is it possible to format output with write.table? I can left-align columns using tab,

  • 0

Is it possible to format output with write.table?

I can left-align columns using tab, sep = '\t', and can increase spacing between columns using two tabs, sep = '\t\t'.

Ideally I would like to be able to right-align columns and use an intermediate amount of spacing than that provided
by ‘\t’ and ‘\t\t’. Using something like sep = '\t ' completely destroyes column alignment.

I must proof a large amount of data extracted from many different files that use numerous different table formats. Closely matching column spacing of R’s output text files to that in the original pdf documents would substantially increase speed and accuracy of proofing.

# example data to write to text file

aa = matrix(c(1000,110,10,1, 
              0,2000,20,2, 
              30,300,3000,30000), nrow=3, byrow=TRUE,
         dimnames = list(NULL, c("C1", "C2", "C3","C4")))
aa

# left align columns using a tab

write.table(aa,file="c:/users/mark w miller/simple r programs/formatted_tablea.txt", na = 'NA', sep = '\t',
row.names = F, col.names = F)

#   1000    110 10  1
#   0   2000    20  2
#   30  300 3000    30000

# increase spacing between columns with two tabs

write.table(aa,file="c:/users/mark w miller/simple r programs/formatted_tableb.txt", na = 'NA', sep = '\t\t',
row.names = F, col.names = F)

#   1000        110     10      1
#   0       2000        20      2
#   30      300     3000        30000

# Here is an example of the desired right-aligned output 
# format with an intermediate amount of spacing 
# (3 spaces vs the 1 or 7 spaces used above):

#   1000    110     10       1
#      0   2000     20       2
#     30    300   3000   30000

# Maybe use cat somehow? 

cat(file="c:/users/mark w miller/simple r programs/formatted_tablec.txt", aa, sep=c(' ', ' ', '\n'))

Or must I write to a csv file and open the output file with Excel? I would prefer to avoid Excel. Or perhaps I must create the output data in the desired format using LaTex?

Sorry for all of the questions this week.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T08:49:50+00:00Added an answer on June 6, 2026 at 8:49 am

    See if the combination of capture.output and the print.gap argument to print is enough:

    capture.output( print(aa, print.gap=3), file="capture.txt")
    
             C1     C2     C3      C4
    [1,]   1000    110     10       1
    [2,]      0   2000     20       2
    [3,]     30    300   3000   30000
    

    An alternate strategy would be to use write.matrix with a sep=" " (3 spaces) argument. You will need to ignore the column headers which will not come out correctly:

    require(MASS)
    write.matrix(aa, file="capturemat.txt", sep="   ")
    
    C1   C2   C3   C4
     1000     110      10       1
        0    2000      20       2
       30     300    3000   30000
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good day, I have been searching for a way to format output using writeline/write
Is it possible using StAX (specifically woodstox) to format the output xml with newlines
Possible Duplicates: Is String.Format as efficient as StringBuilder C# String output: format or concat?
Possible Duplicate: Create Excel file in Java How to save output in excel format
Is it possible to format price according to rules like this using DecimalFormat in
Possible Duplicate: How can I format a nullable DateTime with ToString()? got issue parsing
Is it possible to format powershell output so that it renders as a collapsible
Is it possible to format a number to 2 decimal places with Smarty PHP?
CAST(IH.InvoiceDate AS date) AS InvoiceDate this returns 2012-03-16 is it possible to format this
My application sends Facebook notifications to a user. Is that possible to format the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.