title: formatting, Sprintf in R
body: <p>I am very new to this site and I am having an issue using sprintf in R. Briefly what I am trying to do is the following:</p>
  
  <p>I need to create a text file with a header (which is space delimited and has to maintain that particular space), below which I need to copy some numbers made of X rows (depending on the data, I will read a big table made of thousands of line, but for each ID there will be a variable number of rows; this is not a major problem as I can loop through them). My problem is that i cannot align the numbers below the header. </p>
  
  <pre><code>setwd("C:\\Example\\formatting")
  </code></pre>
  
  <p>My data are in a CSV format so I read:</p>
  
  <pre><code>s100 = read.csv("example.csv", header=T)
  </code></pre>
  
  <p>Then I take the columns I am interested in and transform it in this way:</p>
  
  <pre><code>SID1 = as.vector(as.matrix(s100$Row1))
  SID2 = as.vector(as.matrix(s100$Row2))
  SID3 = as.vector(as.matrix(s100$Row3))
  SIDN = as.vector(as.matrix(s100$RowN))
  </code></pre>
  
  <p>Then I have the following (do not worry about the letters, that part up to a certain point is really easy, I got stuck at the end when I need to read the SID:</p>
  
  <pre><code>sink("Example.xxx", append = T)
  cat("*Some description goes here\n")
  </code></pre>
  
  <p>This goes on an on until I need to put the numbers down. So, when I arrive to this piece:</p>
  
  <pre><code>cat("@  SAB   SSD   TAR.....", sep = "\n")
  </code></pre>
  
  <p>I now need to have aligned the numbers under SAB, SSD, TAR... and so on.
  So, now I do the following ( I only tried using one column and one header first):</p>
  
  <pre><code>cat("SAB ", sep = "\n")
  cat(sprintf("%s\n", SID1, sep="\n" ))
  </code></pre>
  
  <p>But, what I get in the end is the following:</p>
  
  <pre><code>SAB
  0.30
   0.40
   0.50
  </code></pre>
  
  <p>Instead of </p>
  
  <pre><code> SAB    SSD     TAR
  0.30   0.40      10
  0.40   0.80      40
  0.50   0.90      00
  ....   ....      ...
  </code></pre>
  
  <p>So my two questions are:</p>
  
  <ol>
  <li>How to solve the above problem?</li>
  <li>Since at the beginning of that header I have a "@" spaced before the "SAB" how do I align all my numbers accordingly? </li>
  </ol>
  
  <p>I hope I have been clear and not messy, it seems as a simple solution but my knowledge of R and programming go only up to a certain point. </p>
  
  <p>Thank you in advance for any help!</p>