write.fwf.html 9.97 KB
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><title>R: Write object in fixed width format</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" type="text/css" href="../../R.css">
</head><body>

<table width="100%" summary="page for write.fwf {gdata}"><tr><td>write.fwf {gdata}</td><td align="right">R Documentation</td></tr></table>
<h2>Write object in fixed width format</h2>


<h3>Description</h3>

<p>
<code>write.fwf</code> writes object in *f*ixed *w*idth *f*ormat.
</p>


<h3>Usage</h3>

<pre>

write.fwf(x, file="", append=FALSE, quote=FALSE, sep=" ", na="",
  rownames=FALSE, colnames=TRUE, rowCol=NULL, justify="right",
  formatInfo=FALSE, quoteInfo=TRUE, ...)

</pre>


<h3>Arguments</h3>

<table summary="R argblock">
<tr valign="top"><td><code>x</code></td>
<td>
data.frame or matrix, the object to be written</td></tr>
<tr valign="top"><td><code>file</code></td>
<td>
character, name of file or connection, look in
<code><a href="../../utils/html/write.table.html">write.table</a></code> for more</td></tr>
<tr valign="top"><td><code>append</code></td>
<td>
logical, append to existing data in <code>file</code></td></tr>
<tr valign="top"><td><code>quote</code></td>
<td>
logical, quote data in output</td></tr>
<tr valign="top"><td><code>na</code></td>
<td>
character, the string to use for missing values
i.e. <code>NA</code> in the output</td></tr>
<tr valign="top"><td><code>sep</code></td>
<td>
character, separator between columns in output</td></tr>
<tr valign="top"><td><code>rownames</code></td>
<td>
logical, print row names</td></tr>
<tr valign="top"><td><code>colnames</code></td>
<td>
logical, print column names</td></tr>
<tr valign="top"><td><code>rowCol</code></td>
<td>
character, rownames column name</td></tr>
<tr valign="top"><td><code>justify</code></td>
<td>
character, allignment of character columns</td></tr>
<tr valign="top"><td><code>formatInfo</code></td>
<td>
logical, return information on number of levels,
widths and format</td></tr>
<tr valign="top"><td><code>quoteInfo</code></td>
<td>
logical, should <code>formatInfo</code> account for quotes</td></tr>
<tr valign="top"><td><code>...</code></td>
<td>
further arguments to <code><a href="../../base/html/format.info.html">format.info</a></code>,
<code><a href="../../tutoR/html/format.html">format</a></code> and <code><a href="../../utils/html/write.table.html">write.table</a></code></td></tr>
</table>

<h3>Details</h3>

<p>
Output is similar to <code>print(x)</code> or <code>format(x)</code>. Formating is
done completely by <code><a href="../../tutoR/html/format.html">format</a></code> on a column basis. Columns in
the output are by default separated with a space i.e. empty column with
a width of one character, but that can be changed with <code>sep</code>
argument as passed to <code><a href="../../utils/html/write.table.html">write.table</a></code> via ....
</p>
<p>
<code>quote</code> can be used to quote fields in the output. Since all
columns of <code>x</code> are converted to character during the output, all
columns will be quoted! The following is needed for <code>read.fwf</code> or
any other tools outside <font face="Courier New,Courier" color="#666666"><b>R</b></font>. If quotes are used, <code><a href="../../utils/html/read.table.html">read.table</a></code>
can be easily used to read the data back into <font face="Courier New,Courier" color="#666666"><b>R</b></font>. Check examples. Do read
details on <code>quoteInfo</code>.
</p>
<p>
Use only *true* character i.e. not "t" or similar for <code>sep</code> as
number of characters in <code>sep</code> is needed internally.
</p>
<p>
Use <code>na</code> to convert missing/unknown values. Only single value can
be specified. Take a look at <code><a href="unknown.html">NAToUnknown</a></code> if you need
greater flexibility.
</p>
<p>
If <code>rowCol</code> is not <code>NULL</code> and <code>rownames=TRUE</code> rownames
will also have column name with <code>rowCol</code> value. This is mainly for
flexibility with tools outside <font face="Courier New,Courier" color="#666666"><b>R</b></font>. Note that (at least in <font face="Courier New,Courier" color="#666666"><b>R</b></font> 2.4.0) it
is not "easy" to import data back to <font face="Courier New,Courier" color="#666666"><b>R</b></font> with <code><a href="../../utils/html/read.fwf.html">read.fwf</a></code> if
you also export rownames. That is the reason, that default is
<code>rownames=FALSE</code>.
</p>
<p>
Information about format of output can be returned if
<code>formatInfo=TRUE</code>. Returned value is described in value
section. Result is provided by <code><a href="../../base/html/format.info.html">format.info</a></code> and care was
taken to handle numeric properly. If output contains rownames, returned
value accounts for this. Additionally, if <code>rowCol</code> is not
<code>NULL</code> then returned value contains also information about format
of rownames.
</p>
<p>
If <code>quote=TRUE</code> output is wider due to quotes. Return value (with
<code>formatInfo=TRUE</code>) can account for this in two ways; controlled
with argument <code>quoteInfo</code>. However, note that there is no way to
properly read data back to <font face="Courier New,Courier" color="#666666"><b>R</b></font> if <code>quote=TRUE &amp; quoteInfo=FALSE</code> was
specifed for export. <code>quoteInfo</code> applies only when
<code>quote=TRUE</code>. Assume there is a file with quoted data as shown
bellow (column numbers in first three line are only for demonstration of
the values in the output).
</p>
<pre>
123456789 12345678 # for position
123 1234567 123456 # for width with quoteInfo=TRUE
 1   12345   1234  # for width with quoteInfo=FALSE
"a" "hsgdh" "   9"
" " "   bb" " 123"
</pre>
</p>
<p>
With <code>quoteInfo=TRUE</code> <code>write.fwf</code> will return (symbolically)
</p>
<pre>
colname position width
V1             1     3
V2             5     7
V3            13     6
</pre>
</p>
<p>
or (with <code>quoteInfo=FALSE</code>)
</p>
<pre>
colname position width
V1             2     1
V2             6     5
V3            14     4
</pre>
</p>


<h3>Value</h3>

<p>
Besides its effect to write/export data <code>write.fwf</code> can provide
information on format and width. A data.frame is returned with the
following columns:
</p>
<table summary="R argblock">
<tr valign="top"><td><code>colname</code></td>
<td>
name of the column</td></tr>
<tr valign="top"><td><code>nlevels</code></td>
<td>
number of unique values (unused levels of factors are
dropped), 0 for numeric column</td></tr>
<tr valign="top"><td><code>position</code></td>
<td>
starting column number in the output</td></tr>
<tr valign="top"><td><code>width</code></td>
<td>
width of the column</td></tr>
<tr valign="top"><td><code>digits</code></td>
<td>
number of digits after the decimal point</td></tr>
<tr valign="top"><td><code>exp</code></td>
<td>
width of exponent in exponential representation; 0 means
there is no exponential representation, while 1 represents exponent
of length one i.e. <code>1e+6</code> and 2 <code>1e+06</code> or <code>1e+16</code></td></tr>
</table>

<h3>Author(s)</h3>

<p>
Gregor Gorjanc
</p>


<h3>See Also</h3>

<p>
<code><a href="../../base/html/format.info.html">format.info</a></code>, <code><a href="../../tutoR/html/format.html">format</a></code>,
<code><a href="unknown.html">NAToUnknown</a></code>, <code><a href="../../utils/html/write.table.html">write.table</a></code>,
<code><a href="../../utils/html/read.fwf.html">read.fwf</a></code>, <code><a href="../../utils/html/read.table.html">read.table</a></code> and
<code><a href="trim.html">trim</a></code>
</p>


<h3>Examples</h3>

<pre>

  ## Some data
  testData &lt;- data.frame(num1=c(1:10, NA),
                         num2=c(NA, seq(from=1, to=5.5, by=0.5)),
                         num3=c(NA, rnorm(n=10, mean=1e6, sd=3e5)),
                         int1=c(as.integer(1:4), NA, as.integer(5:10)),
                         fac1=factor(c(NA, letters[1:10])),
                         fac2=factor(c(letters[6:15], NA)),
                         cha1=c(letters[17:26], NA),
                         cha2=c(NA, letters[26:17]),
                         stringsAsFactors=FALSE)
  levels(testData$fac1) &lt;- c(levels(testData$fac1), "unusedLevel")
  testData$Date &lt;- as.Date("1900-1-1")
  testData$Date[2] &lt;- NA
  testData$POSIXt &lt;- as.POSIXct(strptime("1900-1-1 01:01:01",
                                         format="%Y-%m-%d %H:%M:%S"))
  testData$POSIXt[5] &lt;- NA

  ## Default
  write.fwf(x=testData)

  ## NA should be - or ------------
  write.fwf(x=testData, na="-")
  write.fwf(x=testData, na="------------")

  ## Some other separator than space
  write.fwf(x=testData[, 1:4], sep="-mySep-")

  ## Write to file and report format and fixed width information
  file &lt;- tempfile("test.txt")
  formatInfo &lt;- write.fwf(x=testData, file=file, formatInfo=TRUE)

  ## Read exported data back to R (note +1 due to separator)
  ## ... without header
  read.fwf(file=file, widths=formatInfo$width + 1, header=FALSE, skip=1,
           strip.white=TRUE)

  ## ... with header - via postimport modfication
  tmp &lt;- read.fwf(file=file, widths=formatInfo$width + 1, skip=1,
                  strip.white=TRUE)
  colnames(tmp) &lt;- read.table(file=file, nrow=1, as.is=TRUE)
  tmp

  ## ... with header - persuading read.fwf to accept header properly
  ## (thanks to Marc Schwartz)
  read.fwf(file=file, widths=formatInfo$width + 1, strip.white=TRUE,
           skip=1, col.names=read.table(file=file, nrow=1, as.is=TRUE))

  ## ... with header - with the use of quotes
  write.fwf(x=testData, file=file, quote=TRUE)
  read.table(file=file, header=TRUE, strip.white=TRUE)

  ## Tidy up
  unlink(file)
</pre>



<hr><div align="center">[Package <em>gdata</em> version 2.3.1 <a href="00Index.html">Index]</a></div>

</body></html>